Created
September 19, 2017 13:50
-
-
Save kleczkowski/8d39703f5a1c6a8a263aa6bfed8845e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MIT License | |
* | |
* Copyright (c) 2017 Konrad Kleczkowski | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package com.github.repaj.libjson.io; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Nested; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.EnumSource; | |
import static org.junit.jupiter.api.Assertions.assertThrows; | |
@SuppressWarnings("ALL") | |
class JsonAccepterTest { | |
abstract class AbstractWhenArrayLiteral { | |
private JsonTokenType type; | |
protected AbstractWhenArrayLiteral(JsonTokenType type) { | |
this.type = type; | |
} | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.BOOLEAN); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"END_ARRAY", "COMMA"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"BEGIN_OBJECT", "BEGIN_ARRAY", "COLON", "BOOLEAN", "STRING", "NUMBER", "NULL"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
} | |
abstract class AbstractWhenValueLiteral { | |
private JsonTokenType literal; | |
protected AbstractWhenValueLiteral(JsonTokenType literal) { | |
this.literal = literal; | |
} | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.NUMBER); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"END_OBJECT", "COMMA"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"BEGIN_OBJECT", "BEGIN_ARRAY", "END_ARRAY", | |
"COLON", "BOOLEAN", "STRING", "NUMBER", "NULL"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
} | |
private JsonAccepter accepter; | |
@BeforeEach | |
void setUp() { | |
accepter = new JsonAccepter(); | |
} | |
@Nested | |
class WhenBeginObject { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.BEGIN_OBJECT); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"END_OBJECT", "STRING"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"BEGIN_OBJECT", | |
"BEGIN_ARRAY", "END_ARRAY", | |
"COMMA", "COLON", | |
"BOOLEAN", | |
"NUMBER", "NULL"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
@Nested | |
class WhenEndObject { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.END_OBJECT); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
} | |
@Nested | |
class WhenKey { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.STRING); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"COLON"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"BEGIN_OBJECT", "END_OBJECT", | |
"BEGIN_ARRAY", "END_ARRAY", | |
"COMMA", "STRING", | |
"BOOLEAN", | |
"NUMBER", "NULL"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
@Nested | |
class WhenColon { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.COLON); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"BEGIN_OBJECT", "BEGIN_ARRAY", "BOOLEAN", "STRING", "NUMBER", "NULL"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"COLON", "COMMA", "END_OBJECT", "END_ARRAY"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
@Nested | |
class WhenNumber extends AbstractWhenValueLiteral { | |
public WhenNumber() { | |
super(JsonTokenType.NUMBER); | |
} | |
} | |
@Nested | |
class WhenString extends AbstractWhenValueLiteral { | |
public WhenString() { | |
super(JsonTokenType.STRING); | |
} | |
} | |
@Nested | |
class WhenBoolean extends AbstractWhenValueLiteral { | |
public WhenBoolean() { | |
super(JsonTokenType.BOOLEAN); | |
} | |
} | |
@Nested | |
class WhenNull extends AbstractWhenValueLiteral { | |
public WhenNull() { | |
super(JsonTokenType.NULL); | |
} | |
} | |
} | |
} | |
} | |
@Nested | |
class WhenBeginArray { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.BEGIN_ARRAY); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"END_ARRAY", "BEGIN_OBJECT", "BEGIN_ARRAY", "STRING", "BOOLEAN", "NUMBER", "NULL"}) | |
void shouldAccept(JsonTokenType tokenType) { | |
accepter.accept(tokenType); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class, | |
names = {"COMMA", "COLON"}) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
@Nested | |
class WhenEndObject { | |
@BeforeEach | |
void setUp() { | |
accepter.accept(JsonTokenType.END_ARRAY); | |
} | |
@ParameterizedTest | |
@EnumSource(value = JsonTokenType.class) | |
void shouldFail(JsonTokenType tokenType) { | |
assertThrows(MalformedJsonException.class, () -> accepter.accept(tokenType)); | |
} | |
} | |
@Nested | |
class WhenBoolean extends AbstractWhenArrayLiteral { | |
public WhenBoolean() { | |
super(JsonTokenType.BOOLEAN); | |
} | |
} | |
@Nested | |
class WhenNumber extends AbstractWhenArrayLiteral { | |
public WhenNumber() { | |
super(JsonTokenType.NUMBER); | |
} | |
} | |
@Nested | |
class WhenString extends AbstractWhenArrayLiteral { | |
public WhenString() { | |
super(JsonTokenType.STRING); | |
} | |
} | |
@Nested | |
class WhenNull extends AbstractWhenArrayLiteral { | |
public WhenNull() { | |
super(JsonTokenType.NULL); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment