1#!/usr/bin/env python 2 3# Copyright 2014 by Sam Mikes. All rights reserved. 4# This code is governed by the BSD license found in the LICENSE file. 5 6import unittest 7 8import os 9import yaml 10import imp 11 12# add parent dir to search path 13import sys 14sys.path.append("src") 15 16import _monkeyYaml as monkeyYaml 17 18class TestMonkeyYAMLParsing(unittest.TestCase): 19 20 def test_empty(self): 21 self.assertEqual(monkeyYaml.load(""), yaml.load("")) 22 23 def test_newline(self): 24 self.assertEqual(monkeyYaml.load("\n"), yaml.load("\n")) 25 26 def test_oneline(self): 27 y = "foo: bar" 28 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 29 30 def test_twolines(self): 31 y = "foo: bar\nbaz_bletch : blith:er" 32 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 33 34 def test_multiLine(self): 35 y = "foo: >\n bar\nbaz: 3" 36 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 37 38 def test_es5id(self): 39 y = "es5id: 15.2.3.6-4-102" 40 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 41 42 def test_Multiline_1(self): 43 lines = [" foo"] 44 value = ">" 45 y = "\n".join([value] + lines) 46 (lines, value) = monkeyYaml.myMultiline(lines, value) 47 self.assertEqual(lines, []) 48 self.assertEqual(value, yaml.load(y)) 49 50 def test_Multiline_2(self): 51 lines = [" foo", " bar"] 52 y = "\n".join([">"] + lines) 53 (lines, value) = monkeyYaml.myMultiline(lines) 54 self.assertEqual(lines, []) 55 self.assertEqual(value, yaml.load(y)) 56 57 def test_Multiline_3(self): 58 lines = [" foo", " bar"] 59 y = "\n".join([">"] + lines) 60 (lines, value) = monkeyYaml.myMultiline(lines) 61 self.assertEqual(lines, []) 62 self.assertEqual(value, yaml.load(y)) 63 64 def test_Multiline_4(self): 65 lines = [" foo", " bar", " other: 42"] 66 (lines, value) = monkeyYaml.myMultiline(lines) 67 self.assertEqual(lines, [" other: 42"]) 68 self.assertEqual(value, "foo bar") 69 70 def test_myLeading(self): 71 self.assertEqual(2, monkeyYaml.myLeadingSpaces(" foo")) 72 self.assertEqual(2, monkeyYaml.myLeadingSpaces(" ")) 73 self.assertEqual(0, monkeyYaml.myLeadingSpaces("\t ")) 74 75 def test_includes_flow(self): 76 y = "includes: [a.js,b.js, c_with_wings.js]\n" 77 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 78 79 def test_myFlowList_1(self): 80 y = "[a.js,b.js, c_with_wings.js, 3, 4.12]" 81 self.assertEqual(monkeyYaml.myFlowList(y), ['a.js', 'b.js', 'c_with_wings.js', 3, 4.12]) 82 83 def test_multiline_list_1(self): 84 y = "foo:\n - bar\n - baz" 85 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 86 87 def test_multiline_list2(self): 88 self.assertEqual(monkeyYaml.myRemoveListHeader(2, " - foo"), "foo") 89 90 def test_multiline_list3(self): 91 (lines, value) = monkeyYaml.myMultilineList([" - foo", " - bar", "baz: bletch"], "") 92 self.assertEqual(lines, ["baz: bletch"]) 93 self.assertEqual(value, ["foo", "bar"]) 94 95 def test_multiline_list_carriage_return(self): 96 y = "foo:\r\n - bar\r\n - baz" 97 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 98 99 def test_oneline_indented(self): 100 y = " foo: bar\n baz: baf\n" 101 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 102 103 104 def test_indentation_215(self): 105 self.maxDiff = None 106 y = """ 107 description: > 108 The method should exist on the Array prototype, and it should be writable 109 and configurable, but not enumerable. 110 includes: [propertyHelper.js] 111 es6id: 22.1.3.13 112 """ 113 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 114 115 def test_indentation_215_2(self): 116 self.maxDiff = None 117 y = """ 118 description: > 119 The method should exist 120 includes: [propertyHelper.js] 121 es6id: 22.1.3.13 122 """ 123 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 124 125 def test_line_folding(self): 126 self.maxDiff = None 127 y = """ 128description: aaa 129 bbb 130es6id: 19.1.2.1 131""" 132 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 133 134 def test_line_folding_2(self): 135 self.maxDiff = None 136 y = """ 137description: ccc 138 139 ddd 140 141es6id: 19.1.2.1 142""" 143 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 144 145 def test_line_folding_3(self): 146 self.maxDiff = None 147 y = """ 148description: eee 149 150 151 fff 152es6id: 19.1.2.1 153""" 154 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 155 156 def test_line_folding_4(self): 157 self.maxDiff = None 158 y = """ 159description: ggg 160 161 hhh 162 iii 163 164 jjj 165es6id: 19.1.2.1 166""" 167 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 168 169 def test_no_folding(self): 170 y = """ 171description: | 172 This is text that, naively parsed, would appear 173 174 to: have 175 nested: data 176""" 177 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 178 179 def test_value_multiline(self): 180 y = """ 181description: 182 This is a multi-line value 183 184 whose trailing newline should be stripped 185""" 186 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 187 188 def test_nested_1(self): 189 y = """ 190es61d: 19.1.2.1 191negative: 192 stage: early 193 type: ReferenceError 194description: foo 195""" 196 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 197 198 def test_nested_2(self): 199 y = """ 200es61d: 19.1.2.1 201first: 202 second_a: 203 third: 1 204 second_b: 3 205description: foo 206""" 207 self.assertEqual(monkeyYaml.load(y), yaml.load(y)) 208 209if __name__ == '__main__': 210 unittest.main() 211