| 6 | | class TestTokens(test_appliance.TestAppliance): |
| | 24 | _replaces = { |
| | 25 | yaml.DirectiveToken: '%', |
| | 26 | yaml.DocumentStartToken: '---', |
| | 27 | yaml.DocumentEndToken: '...', |
| | 28 | yaml.AliasToken: '*', |
| | 29 | yaml.AnchorToken: '&', |
| | 30 | yaml.TagToken: '!', |
| | 31 | yaml.ScalarToken: '_', |
| | 32 | yaml.BlockSequenceStartToken: '[[', |
| | 33 | yaml.BlockMappingStartToken: '{{', |
| | 34 | yaml.BlockEndToken: ']}', |
| | 35 | yaml.FlowSequenceStartToken: '[', |
| | 36 | yaml.FlowSequenceEndToken: ']', |
| | 37 | yaml.FlowMappingStartToken: '{', |
| | 38 | yaml.FlowMappingEndToken: '}', |
| | 39 | yaml.BlockEntryToken: ',', |
| | 40 | yaml.FlowEntryToken: ',', |
| | 41 | yaml.KeyToken: '?', |
| | 42 | yaml.ValueToken: ':', |
| | 43 | } |
| 8 | | # Tokens mnemonic: |
| 9 | | # directive: % |
| 10 | | # document_start: --- |
| 11 | | # document_end: ... |
| 12 | | # alias: * |
| 13 | | # anchor: & |
| 14 | | # tag: ! |
| 15 | | # scalar _ |
| 16 | | # block_sequence_start: [[ |
| 17 | | # block_mapping_start: {{ |
| 18 | | # block_end: ]} |
| 19 | | # flow_sequence_start: [ |
| 20 | | # flow_sequence_end: ] |
| 21 | | # flow_mapping_start: { |
| 22 | | # flow_mapping_end: } |
| 23 | | # entry: , |
| 24 | | # key: ? |
| 25 | | # value: : |
| | 45 | def test_tokens(data_filename, tokens_filename, verbose=False): |
| | 46 | tokens1 = [] |
| | 47 | tokens2 = open(tokens_filename, 'rb').read().split() |
| | 48 | try: |
| | 49 | for token in yaml.scan(open(data_filename, 'rb')): |
| | 50 | if not isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)): |
| | 51 | tokens1.append(_replaces[token.__class__]) |
| | 52 | finally: |
| | 53 | if verbose: |
| | 54 | print "TOKENS1:", ' '.join(tokens1) |
| | 55 | print "TOKENS2:", ' '.join(tokens2) |
| | 56 | assert len(tokens1) == len(tokens2), (tokens1, tokens2) |
| | 57 | for token1, token2 in zip(tokens1, tokens2): |
| | 58 | assert token1 == token2, (token1, token2) |
| 27 | | replaces = { |
| 28 | | DirectiveToken: '%', |
| 29 | | DocumentStartToken: '---', |
| 30 | | DocumentEndToken: '...', |
| 31 | | AliasToken: '*', |
| 32 | | AnchorToken: '&', |
| 33 | | TagToken: '!', |
| 34 | | ScalarToken: '_', |
| 35 | | BlockSequenceStartToken: '[[', |
| 36 | | BlockMappingStartToken: '{{', |
| 37 | | BlockEndToken: ']}', |
| 38 | | FlowSequenceStartToken: '[', |
| 39 | | FlowSequenceEndToken: ']', |
| 40 | | FlowMappingStartToken: '{', |
| 41 | | FlowMappingEndToken: '}', |
| 42 | | BlockEntryToken: ',', |
| 43 | | FlowEntryToken: ',', |
| 44 | | KeyToken: '?', |
| 45 | | ValueToken: ':', |
| 46 | | } |
| | 60 | test_tokens.unittest = ['.data', '.tokens'] |
| 52 | | tokens1 = [] |
| 53 | | for token in scan(file(data_filename, 'rb')): |
| 54 | | if not isinstance(token, (StreamStartToken, StreamEndToken)): |
| 55 | | tokens1.append(token) |
| 56 | | tokens1 = [self.replaces[t.__class__] for t in tokens1] |
| 57 | | self.failUnlessEqual(tokens1, tokens2) |
| 58 | | except: |
| 59 | | print |
| 60 | | print "DATA:" |
| 61 | | print file(data_filename, 'rb').read() |
| 62 | | print "TOKENS1:", tokens1 |
| 63 | | print "TOKENS2:", tokens2 |
| 64 | | raise |
| | 66 | for token in yaml.scan(open(filename, 'rb')): |
| | 67 | tokens.append(token.__class__.__name__) |
| | 68 | finally: |
| | 69 | if verbose: |
| | 70 | pprint.pprint(tokens) |
| 70 | | def _testScanner(self, test_name, data_filename, canonical_filename): |
| 71 | | for filename in [canonical_filename, data_filename]: |
| 72 | | tokens = None |
| 73 | | try: |
| 74 | | tokens = [] |
| 75 | | for token in scan(file(filename, 'rb')): |
| 76 | | if not isinstance(token, (StreamStartToken, StreamEndToken)): |
| 77 | | tokens.append(token.__class__.__name__) |
| 78 | | except: |
| 79 | | print |
| 80 | | print "DATA:" |
| 81 | | print file(data_filename, 'rb').read() |
| 82 | | print "TOKENS:", tokens |
| 83 | | raise |
| 84 | | |
| 85 | | TestScanner.add_tests('testScanner', '.data', '.canonical') |
| 86 | | |