-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
221 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'package:robots_txt/robots_txt.dart'; | ||
|
||
Future<void> main() async { | ||
// Validating an invalid file will throw a `FormatException`. | ||
try { | ||
Robots.validate('This is obviously an invalid robots.txt file.'); | ||
} on FormatException { | ||
print('As expected, the first file is flagged as invalid.'); | ||
} | ||
|
||
// Validating an already valid file. | ||
try { | ||
Robots.validate(''' | ||
User-agent: * | ||
Disallow: / | ||
Allow: /file.txt | ||
Sitemap: https://example.com/sitemap.xml | ||
'''); | ||
print('As expected also, the second file is not flagged as invalid.'); | ||
} on FormatException { | ||
print('Welp, this was not supposed to happen.'); | ||
} | ||
|
||
late final String contentsFromBefore; | ||
|
||
// Validating a file with unsupported fields. | ||
try { | ||
Robots.validate( | ||
contentsFromBefore = ''' | ||
User-agent: * | ||
Some-field: abcd.txt | ||
''', | ||
); | ||
} on FormatException { | ||
print( | ||
'This file is invalid on the grounds that it contains fields we did not ' | ||
'expect it to have.', | ||
); | ||
print( | ||
"Let's fix that by including the custom field in the call to validate().", | ||
); | ||
try { | ||
Robots.validate(contentsFromBefore, allowedFieldNames: {'Some-field'}); | ||
print('Aha! Now there are no issues.'); | ||
} on FormatException { | ||
print('Welp, this also was not supposed to happen.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/// Lightweight, fully documented `robots.txt` file parser. | ||
library robots_txt; | ||
|
||
export 'src/parser.dart' show Robots, PrecedentRuleType, FieldType; | ||
export 'src/robots.dart' show Robots, PrecedentRuleType, FieldType; | ||
export 'src/rule.dart' show Rule, FindRule, Precedence, PrecedenceStrategy; | ||
export 'src/ruleset.dart' show Ruleset, FindRuleInRuleset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:test/test.dart'; | ||
|
||
import 'package:robots_txt/robots_txt.dart'; | ||
|
||
import '../samples.dart'; | ||
|
||
void main() { | ||
group('The validator correctly deals with', () { | ||
test('an empty file.', () { | ||
expect(() => Robots.validate(emptyContents), returnsNormally); | ||
}); | ||
|
||
test('an invalid file.', () { | ||
expect(() => Robots.validate(invalidContents), throwsFormatException); | ||
}); | ||
|
||
test('a valid file with a custom field name not accounted for.', () { | ||
expect( | ||
() => Robots.validate(validContentsWithCustomFieldName), | ||
throwsFormatException, | ||
); | ||
}); | ||
|
||
test('a valid file with a custom field name.', () { | ||
expect( | ||
() => Robots.validate( | ||
validContentsWithCustomFieldName, | ||
allowedFieldNames: {'Field'}, | ||
), | ||
returnsNormally, | ||
); | ||
}); | ||
|
||
test('a valid file with an invalid path definition.', () { | ||
expect( | ||
() => Robots.validate(validContentsInvalidPattern), | ||
throwsFormatException, | ||
); | ||
}); | ||
|
||
test('a valid file.', () { | ||
expect( | ||
() => Robots.validate(validContentsValidPattern), | ||
returnsNormally, | ||
); | ||
}); | ||
}); | ||
} |