diff options
Diffstat (limited to 'yamlesque/test/src')
-rw-r--r-- | yamlesque/test/src/BasicTest.scala | 196 | ||||
-rw-r--r-- | yamlesque/test/src/NegTest.scala | 96 | ||||
-rw-r--r-- | yamlesque/test/src/StreamTest.scala | 54 | ||||
-rw-r--r-- | yamlesque/test/src/StringTest.scala | 33 | ||||
-rw-r--r-- | yamlesque/test/src/VerbatimTest.scala | 114 |
5 files changed, 493 insertions, 0 deletions
diff --git a/yamlesque/test/src/BasicTest.scala b/yamlesque/test/src/BasicTest.scala new file mode 100644 index 0000000..4261207 --- /dev/null +++ b/yamlesque/test/src/BasicTest.scala @@ -0,0 +1,196 @@ +package yamlesque + +import utest._ + +object BasicTest extends TestSuite { + def tests = Tests { + "empty doc" - { + read("") ==> Null + } + "empty, terminated doc" - { + read("---") ==> Null + } + "null doc" - { + read("null") ==> Null + } + "plain string" - { + read("a") ==> Str("a") + read("a ") ==> Str("a") + } + "plain int" - { + read("1") ==> Num(1) + } + "plain double" - { + read("1.1") ==> Num(1.1) + } + "combined plain string" - { + read("""|a + |b + |""".stripMargin) ==> Str("a b") + } + "combined plain string, indentation" - { + read("""|a + | b + | c + |d + |""".stripMargin) ==> Str("a b c d") + } + "plain bool" - { + read("true") ==> Bool(true) + read("false") ==> Bool(false) + } + "map, empty" - { + read("a: ") ==> Obj("a" -> Null) + read("a:") ==> Obj("a" -> Null) + read("a:\n") ==> Obj("a" -> Null) + } + "map, single" - { + read("a: b") ==> Obj("a" -> Str("b")) + read("a:\n b") ==> Obj("a" -> Str("b")) + read("a:\n b") ==> Obj("a" -> Str("b")) + } + "map, space in key" - { + read("a : b") ==> Obj("a" -> Str("b")) + read("hello world : b") ==> Obj("hello world" -> Str("b")) + } + "map, multiple" - { + read("""|a: x + |b: + | y + |c: + | foo + |""".stripMargin) ==> Obj( + "a" -> Str("x"), + "b" -> Str("y"), + "c" -> Str("foo") + ) + } + "map, nested" - { + read("""|a: + | b: x + |b: a: foo + | b: bar + |c: y + |""".stripMargin) ==> Obj( + "a" -> Obj("b" -> Str("x")), + "b" -> Obj( + "a" -> Str("foo"), + "b" -> Str("bar") + ), + "c" -> Str("y") + ) + } + "list, empty" - { + read("- ") ==> Arr(Null) + read("-") ==> Arr(Null) + read("-\n") ==> Arr(Null) + } + "list, single" - { + read("- a") ==> Arr(Str("a")) + read("-\n a") ==> Arr(Str("a")) + read("-\n a") ==> Arr(Str("a")) + } + "list, multiple" - { + read("""|- a + |- + | b + |- c + |""".stripMargin) ==> Arr(Str("a"), Str("b"), Str("c")) + } + "list, nested" - { + read("""|- a + |- - b1 + | - b2 + |- + | - - c1 + | - c2 + |""".stripMargin) ==> Arr( + Str("a"), + Arr(Str("b1"), Str("b2")), + Arr( + Arr("c1"), + Str("c2") + ) + ) + } + "list after map" - { + read("""|a: + | - b + | - c + |""".stripMargin) ==> Obj( + "a" -> Arr(Str("b"), Str("c")) + ) + } + "list after map, no indent" - { + read("""|a: + |- b + |- c + |""".stripMargin) ==> Obj( + "a" -> Arr(Str("b"), Str("c")) + ) + } + "comment" - { + read("#nothing to see here") ==> Null + read("# nothing to see here") ==> Null + } + "comment, after string" - { + read("a #nothing to see here") ==> Str("a") + } + "comment, after key" - { + read("a: #nothing to see here") ==> Obj("a" -> Null) + } + "comment, after item" - { + read("- #nothing to see here") ==> Arr(Null) + } + "not a comment" - { + read("a#nothing to see here") ==> Str("a#nothing to see here") + read("a:#nothing to see here") ==> Str("a:#nothing to see here") + read("a-#nothing to see here") ==> Str("a-#nothing to see here") + } + "mixed" - { + read("""|# Authentication config + |auth: + | username: john doe + | password: + | guest + | 2fa: + | - otp: a1234 + | - + | code: abc + | - other: backdoor! # super secret back door + | + |# Interface to listen on + |# + |# Multiple are allowed + |# + |interfaces: + | - addr: 0.0.0.0 + | port: 1234 + | - addr: 0.0.0.0 + | port: 80 + |extra: null + |""".stripMargin) ==> Obj( + "auth" -> Obj( + "username" -> Str("john doe"), + "password" -> Str("guest"), + "2fa" -> Arr( + Obj("otp" -> Str("a1234")), + Obj("code" -> Str("abc")), + Obj("other" -> Str("backdoor!")) + ) + ), + "interfaces" -> Arr( + Obj( + "addr" -> Str("0.0.0.0"), + "port" -> Num(1234) + ), + Obj( + "addr" -> Str("0.0.0.0"), + "port" -> Num(80) + ) + ), + "extra" -> Null + ) + } + } +} diff --git a/yamlesque/test/src/NegTest.scala b/yamlesque/test/src/NegTest.scala new file mode 100644 index 0000000..627ef0d --- /dev/null +++ b/yamlesque/test/src/NegTest.scala @@ -0,0 +1,96 @@ +package yamlesque + +import utest._ + +object NegTest extends TestSuite { + def tests = Tests { + "key and string" - { + val e = intercept[Parser.ParseException] { + read("""|b: + |a + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + "list and key" - { + val e = intercept[Parser.ParseException] { + read("""|- b: + |a: + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + "list and string" - { + val e = intercept[Parser.ParseException] { + read("""|- + |a + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + "list and key" - { + val e = intercept[Parser.ParseException] { + read("""|- + |a: + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + "key alignment" - { + val e = intercept[Parser.ParseException] { + read("""|a: + | a: + | b: + |""".stripMargin) + } + assert(e.message.contains("aligned")) + } + "list alignment" - { + val e = intercept[Parser.ParseException] { + read("""|- + | - + | - + |""".stripMargin) + } + assert(e.message.contains("aligned")) + } + "verbatim end" - { + val e = intercept[Parser.ParseException] { + read("""|a: | + | foo + | b # b is parsed as a scalar + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + "verbatim before last token" - { + val e = intercept[Parser.ParseException] { + read("""|a: + | a: | + | b + |""".stripMargin) + + } + assert(e.message.contains("expected")) + } + "verbatim before last token 2" - { + val e = intercept[Parser.ParseException] { + read("""|a: + | a: + | a: | + | b: + |""".stripMargin) + } + assert(e.message.contains("aligned")) + } + "verbatim followed by scalar" - { + val e = intercept[Parser.ParseException] { + read("""|| + | a + |a + |""".stripMargin) + } + assert(e.message.contains("expected")) + } + } +} diff --git a/yamlesque/test/src/StreamTest.scala b/yamlesque/test/src/StreamTest.scala new file mode 100644 index 0000000..e739d65 --- /dev/null +++ b/yamlesque/test/src/StreamTest.scala @@ -0,0 +1,54 @@ +package yamlesque + +import utest._ +import java.io.StringReader + +// test multiple documents +object StreamTest extends TestSuite { + def tests = Tests { + "empty doc" - { + readAll("") ==> Null :: Nil + } + "empty doc, start only" - { + // first --- is optional + readAll("---") ==> Null :: Nil + } + "empty docs" - { + readAll("---\n---") ==> Null :: Null :: Nil + readAll("---\n---\n---") ==> Null :: Null :: Null :: Nil + } + "empty and non-empty docs" - { + val s = """|--- + |a + |--- + """.stripMargin + readAll(s) ==> Str("a") :: Null :: Nil + } + "non-empty doc, implicit start" - { + val s = """|a + |""".stripMargin + readAll(s) ==> Str("a") :: Nil + } + "non-empty doc, explicit start" - { + val s = """|--- + |a + |""".stripMargin + readAll(s) ==> Str("a") :: Nil + } + "non-empty docs, implicit start" - { + val s = """|a + |--- + |b + """.stripMargin + readAll(s) ==> Str("a") :: Str("b") :: Nil + } + "non-empty docs, explicit start" - { + val s = """|--- + |a + |--- + |b + """.stripMargin + readAll(s) ==> Str("a") :: Str("b") :: Nil + } + } +} diff --git a/yamlesque/test/src/StringTest.scala b/yamlesque/test/src/StringTest.scala new file mode 100644 index 0000000..b4ae519 --- /dev/null +++ b/yamlesque/test/src/StringTest.scala @@ -0,0 +1,33 @@ +package yamlesque + +import utest._ + +object StringTest extends TestSuite { + def tests = Tests { + "quoted simple" - { + read(""""a"""") ==> Str("a") + read(""" "a" """) ==> Str("a") + } + "quoted non-strings" - { + read(""""1"""") ==> Str("1") + read(""""1.2"""") ==> Str("1.2") + read(""""true"""") ==> Str("true") + read(""""false"""") ==> Str("false") + read(""""null"""") ==> Str("null") + } + "quoted comment" - { + read(""""#hello"""") ==> Str("#hello") + read(""""a #hello"""") ==> Str("a #hello") + } + "scalar with quote" - { + read(""" a" """) ==> Str("a\"") + read(""" a"hmm" """) ==> Str("a\"hmm\"") + read(""" -"a" """) ==> Str("-\"a\"") + read(""" :"a" """) ==> Str(":\"a\"") + } + "quoted key" - { + read(""" "a # b": a """) ==> Obj("a # b" -> Str("a")) + read(""" "a # b" : a """) ==> Obj("a # b" -> Str("a")) + } + } +} diff --git a/yamlesque/test/src/VerbatimTest.scala b/yamlesque/test/src/VerbatimTest.scala new file mode 100644 index 0000000..5e0b9d6 --- /dev/null +++ b/yamlesque/test/src/VerbatimTest.scala @@ -0,0 +1,114 @@ +package yamlesque + +import utest._ + +object VerbatimTest extends TestSuite { + def tests = Tests { + "empty verbatim" - { + read("|") ==> Str("") + read("|\n") ==> Str("") + } + "single verbatim" - { + read("""|| + | a + |""".stripMargin) ==> Str("a\n") + read("""|| + | a + |""".stripMargin) ==> Str("a\n") + } + "multi-line verbatim" - { + read("""|| + | foo bar + | baz + |""".stripMargin) ==> Str("foo bar\nbaz\n") + } + "multi-line indent verbatim" - { + read("""|| + | foo + | bar + | baz + |""".stripMargin) ==> Str("foo\n bar\nbaz\n") + } + "verbatim in map" - { + read("""|a: | + | foo + | bar + | baz + |b: | + | extra + | cool! + |""".stripMargin) ==> Obj( + "a" -> Str("foo\n bar\nbaz\n"), + "b" -> Str("extra\ncool!\n") + ) + } + "empty verbatim in map" - { + read("""|a: | + |b: + |c: | + |d: |""".stripMargin) ==> Obj( + "a" -> Str(""), + "b" -> Null, + "c" -> Str(""), + "d" -> Str("") + ) + } + "verbatim in list" - { + read("""|- | + | foo + |- | + | extra + | cool! + |""".stripMargin) ==> Arr(Str("foo\n"), Str("extra\ncool!\n")) + } + "empty verbatim in list" - { + read("""|- | + |- + |- | + |- | + |""".stripMargin) ==> Arr(Str(""), Null, Str(""), Str("")) + } + "new lines in verbatim" - { + read("""|a: | + | + | a + | b + | + | + | c + | + | + | + |b: + |""".stripMargin) ==> Obj( + "a" -> Str("\na\nb\n\n\nc\n"), + "b" -> Null + ) + } + "minimum starting col" - { + read("""|a: + | b: | + | c: + |""".stripMargin) ==> Obj( + "a" -> Obj( + "b" -> Str(""), + "c" -> Null + ) + ) + } + "minimum starting col prev" - { + read("""|a: + | a: + | a: | + | b: foo bar + |""".stripMargin) ==> Obj( + "a" -> Obj( + "a" -> Obj( + "a" -> Str("") + ), + "b" -> Str("foo bar") + ) + ) + } + } +} |