summaryrefslogtreecommitdiff
path: root/src/test/scala/com/google/gimd/text/ParserTestCase.scala
blob: d960c177ab6da8d99185f94eadc74845eb8a4009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gimd.text

import org.junit.Test
import org.junit.Assert._
import Parser._

final class ParserTestCase {

  @Test
  def emptyFile() {
    val msg = parse("")
    assertEquals(Message(), msg)
  }

  @Test{val expected = classOf[ParserException]}
  def LFasOnlyConent {
    parse(new java.io.StringReader("\n"))
  }

  @Test
  def oneFieldMessage {
    val msg = parse("fieldName value\n")
    assertEquals(Message(Field("fieldName", "value")), msg)
  }

  @Test
  def twoFieldMessage {
    val input = """|firstName John
                   |lastName Doe
                   |""".stripMargin
    val msg = parse(input)
    val expected = Message(Field("firstName", "John"), Field("lastName", "Doe"))
    assertEquals(expected, msg)
  }

  @Test{val expected = classOf[ParserException]}
  def fieldNameStartingWithNumber {
    parse(new java.io.StringReader("12fieldName 0\n"))
  }

  @Test{val expected = classOf[ParserException]}
  def fieldNameContainingNonLatinCharacter {
    parse(new java.io.StringReader("fieldżółty 0\n"))
  }

  @Test{val expected = classOf[ParserException]}
  def angleBracketsDoNotMatch {
    val input = """|firstName John
                   |nested1 <
                   |  nested2 <
                   |    nested3 <
                   |  >
                   |  nested4 <
                      >
                   |>
                   |
                   |""".stripMargin
    parse(input)
  }

  @Test
  def bareStringWithSpacesAsPrefix {
    val msg = parse("field   Prefix of two spaces\n")
    assertEquals(Message(Field("field", "  Prefix of two spaces")), msg)
  }

  @Test
  def bareStringWithNumberAsPrefix {
    val msg = parse("field 12.02 Prefix of two spaces\n")
    assertEquals(Message(Field("field", "12.02 Prefix of two spaces")), msg)
  }

  @Test
  def bareStringWithQuotedStringAsSuffix {
    val msg = parse("field Here comes quoted \"word\"\n")
    assertEquals(Message(Field("field", "Here comes quoted \"word\"")), msg)
  }

  @Test
  def bareStringWithBackslashAsPrefix {
    val msg = parse("field \\Backslash as prefix?\n")
    assertEquals(Message(Field("field", "\\Backslash as prefix?")), msg)
  }

  @Test{val expected = classOf[ParserException]}
  def bareStringWithUnescapedCharacter {
    parse("field Here comes tab:"+18.toChar+"\n")
  }

  @Test
  def emptyStringValue {
    val msg = parse("field \"\"\n")
    assertEquals(Message(Field("field", "")), msg)
  }

  @Test{val expected = classOf[ParserException]}
  def doesNotNeedQuotingString {
    parse("field \"This string doesn't need quoting!\"\n")
  }

  @Test
  def multiLineString {
    val input = """|lovePoem "
                   |  Hi my dear,
                   |  I haven't written to you because I had no ink!
                   |  You can imagine how I suffered from that situation. :(
                   |"
                   |""".stripMargin
    val expectedVal = """|Hi my dear,
                         |I haven't written to you because I had no ink!
                         |You can imagine how I suffered from that situation. :(""".stripMargin
    val msg = parse(input)
    assertEquals(Message(Field("lovePoem", expectedVal)), msg)
  }

  @Test
  def quotedSingleLineString {
    val msg = parse("field \"Here's quoted string with strange character: \\x0b\"\n")
    val expected = Message(Field("field", "Here's quoted string with strange character: "+11.toChar))
    assertEquals(expected, msg)
  }

  @Test{val expected = classOf[ParserException]}
  def wrongIndentionForMultiLineString {
    val input = """|level1 < "
                   |  level2 "
                   |    This is correctly intended
                   |  But this is not!
                   |  "
                   |>
                   |""".stripMargin
    parse(input)
  }

  @Test
  def indentionForMultiLineStringInNestedStructure {
    val input = """|level1 <
                   |  level2 <
                   |    msg "
                   |      This is correctly intended
                   |      And this is one too!
                   |    "
                   |  >
                   |>
                   |""".stripMargin
    val msg = parse(input)
    val expected =
    Message(Field("level1",
      Message(Field("level2",
        Message(Field("msg", "This is correctly intended\nAnd this is one too!"))
      ))
    ))
    assertEquals(expected, msg)
  }

  @Test
  def zeroValue {
    val msg = parse("field 0\n")
    assertEquals(Message(Field("field", 0)), msg)
  }

  @Test
  def decimalValueBetweenMinusOneAndZero {
    val msg = parse("field -0.334\n")
    assertEquals(Message(Field("field", BigDecimal("-0.334"))), msg)
  }

  @Test
  def decimalValueBetweenZeroAndOne {
    val msg = parse("field 0.012\n")
    assertEquals(Message(Field("field", BigDecimal("0.012"))), msg)
  }

  @Test
  def decimalValueWithLotsOfDigits {
    val msg = parse("field 34908547234897234623489.324893475783465345653648334\n")
    assertEquals(Message(Field("field", BigDecimal("34908547234897234623489.324893475783465345653648334"))), msg)
  }

  @Test
  def MinusZeroShouldBeString {
    val msg = parse("field -0\n")
    assertEquals(Message(Field("field", "-0")), msg)
  }

  //TODO (1) Add tests for Int and Long specifically
  //TODO (2) Add tests for timestamps

}