/*
 * Copyright 2012-2015 JetBrains s.r.o
 *
 * 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 jetbrains.jetpad.json;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ParsingTest {
  public ParsingTest() {
  }

  @Test
  public void emptyObject() {
    JsonObject object = (JsonObject) JsonParser.parse("{}");

    assertTrue(object.getKeys().isEmpty());
  }

  @Test
  public void extraSymbols() {
    try {
      JsonParser.parse("{} {");
      Assert.fail();
    } catch (JsonParsingException e) {
    }
  }

  @Test
  public void unknownSymbols() {
    try {
      JsonParser.parse("{} @");
      Assert.fail();
    } catch (JsonParsingException e) {
    }
  }

  @Test
  public void emptyArray() {
    JsonArray array = (JsonArray) JsonParser.parse("[]");

    assertTrue(array.size() == 0);
  }

  @Test
  public void nullValue() {
    assertTrue(JsonParser.parse("null") instanceof JsonNull);
  }

  @Test
  public void trueValue() {
    JsonBoolean trueValue = (JsonBoolean) JsonParser.parse("true");

    assertTrue(trueValue.getBooleanValue());
  }

  @Test
  public void falseValue() {
    JsonBoolean falseValue = (JsonBoolean) JsonParser.parse("false");

    Assert.assertFalse(falseValue.getBooleanValue());
  }

  @Test
  public void multiElementArray() {
    JsonArray array = (JsonArray) JsonParser.parse("[true, false]");

    assertEquals(2, array.size());
  }

  @Test
  public void simpleNumber() {
    JsonNumber n = (JsonNumber) JsonParser.parse("239");

    assertEquals(239.0, n.getDoubleValue(), 0.000001f);
  }

  @Test
  public void simpleNegativeNumber() {
    JsonNumber n = (JsonNumber) JsonParser.parse("-239");

    assertEquals(-239.0, n.getDoubleValue(), 0.000001f);
  }

  @Test
  public void simpleNumberWithDot() {
    JsonNumber n = (JsonNumber) JsonParser.parse("2.39");

    assertEquals(2.39, n.getDoubleValue(), 0.000001f);
  }

  @Test
  public void numberWithExponent() {
    JsonNumber n = (JsonNumber) JsonParser.parse("2e1");

    assertEquals(20.0, n.getDoubleValue(), 0.000001f);
  }

  @Test
  public void fractionalNumberWithExponent() {
    JsonNumber n = (JsonNumber) JsonParser.parse("2.39e1");

    assertEquals(23.9, n.getDoubleValue(), 0.000001f);
  }

  @Test
  public void simpleString() {
    JsonString s = (JsonString) JsonParser.parse("\"abc\"");

    assertEquals("abc", s.getStringValue());
  }

  @Test
  public void simpleEscapedString() {
    JsonString s = (JsonString) JsonParser.parse("\"a\\nbc\"");

    assertEquals("a\nbc", s.getStringValue());
  }

  @Test
  public void unicodeEscapeSequence() {
    JsonString s = (JsonString) JsonParser.parse("\"\\u000aabc\"");

    assertEquals("\nabc", s.getStringValue());
  }

  @Test
  public void objectParsing() {
    JsonObject obj = (JsonObject) JsonParser.parse("{\"a\":239}");
    JsonNumber number = (JsonNumber) obj.get("a");

    assertEquals(239.0, number.getDoubleValue(), 0.000001f);
  }
}
@@ -795,37 +795,8 @@
 st %7B
-%0A  public ParsingTest() %7B%0A  %7D
 %0A%0A  
@@ -888,25 +888,24 @@
 arse(%22%7B%7D%22);%0A
-%0A
     assertTr
@@ -940,32 +940,71 @@
 ));%0A  %7D%0A%0A  @Test
+(expected = JsonParsingException.class)
 %0A  public void e
@@ -1015,36 +1015,24 @@
 Symbols() %7B%0A
-    try %7B%0A  
     JsonPars
@@ -1055,84 +1055,57 @@
 ;%0A  
-    Assert.fail();%0A    %7D catch (JsonParsingException e) %7B%0A    %7D%0A  %7D%0A%0A  @Test
+%7D%0A%0A  @Test(expected = JsonParsingException.class)
 %0A  p
@@ -1138,20 +1138,8 @@
 ) %7B%0A
-    try %7B%0A  
     
@@ -1168,74 +1168,8 @@
 %22);%0A
-      Assert.fail();%0A    %7D catch (JsonParsingException e) %7B%0A    %7D%0A
   %7D%0A

replace try/catch in ParsingTest with expected exception