Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Missing a strict function test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbelang committed Jan 8, 2020
1 parent a17ea51 commit 9f0fc1b
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.raml</groupId>
<artifactId>raml-parser-parent</artifactId>
<version>1.0.46-SNAPSHOT</version>
<version>1.0.48-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Raml Java Parser 2nd generation parent</name>
<description>Raml parser implemented in java</description>
Expand Down
2 changes: 1 addition & 1 deletion raml-parser-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.raml</groupId>
<artifactId>raml-parser-parent</artifactId>
<version>1.0.46-SNAPSHOT</version>
<version>1.0.48-SNAPSHOT</version>
</parent>
<artifactId>raml-parser-2</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* 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 org.raml.yagi.framework.grammar.rule;

import org.junit.Test;
import org.mockito.Mockito;
import org.raml.yagi.framework.nodes.BooleanNode;
import org.raml.yagi.framework.nodes.StringNodeImpl;
import org.raml.yagi.framework.nodes.jackson.JBooleanNode;
import org.raml.yagi.framework.nodes.snakeyaml.SYBooleanNode;
import org.raml.yagi.framework.nodes.snakeyaml.SYStringNode;

import static org.junit.Assert.*;

/**
* Created. There, you have it.
*/
public class BooleanTypeRuleTest
{

@Test
public void booleanWillMatchString()
{

BooleanTypeRule rule = new BooleanTypeRule(true);
assertFalse(rule.matches(new StringNodeImpl("false")));
assertFalse(rule.matches(new StringNodeImpl("true")));
assertFalse(rule.matches(new StringNodeImpl("anythingElse")));
}

@Test
public void booleanWontMatchString()
{

BooleanTypeRule rule = new BooleanTypeRule(false);
assertTrue(rule.matches(new StringNodeImpl("false")));
assertTrue(rule.matches(new StringNodeImpl("true")));
assertFalse(rule.matches(new StringNodeImpl("anythingElse")));
}

@Test
public void booleanMatchesBoolean()
{

BooleanNode booleanNode = Mockito.mock(BooleanNode.class);
BooleanTypeRule rule = new BooleanTypeRule(true);
assertTrue(rule.matches(booleanNode));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#%RAML 1.0
title: Sample API
types:
TestType:
type: object
properties:
count?: integer
flag?: boolean
name?: string
/test:
post:
body:
application/json:
type: TestType
example: |
{ "count": 12, "flag": "true", "name": "Joe" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
RamlDocumentNode (Start: 11 , End: 291, On: input.raml, Source: SYObjectNode)
KeyValueNodeImpl (Start: 11 , End: 28, On: input.raml)
SYStringNode: "title" (Start: 11 , End: 16, On: input.raml)
OverlayableObjectNodeImpl (Start: 18 , End: 28, On: input.raml, Source: SYStringNode)
KeyValueNodeImpl (Start: 11 , End: 28, On: input.raml)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "Sample API" (Start: 18 , End: 28, On: input.raml)
TypesNode (Start: 29 , End: 144, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "types" (Start: 29 , End: 34, On: input.raml)
SYObjectNode (Start: 38 , End: 144, On: input.raml)
TypeDeclarationField (Start: 38 , End: 144, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "TestType" (Start: 38 , End: 46, On: input.raml)
TypeDeclarationNode (Start: 52 , End: 144, On: input.raml, Source: SYObjectNode)
KeyValueNodeImpl (Start: 52 , End: 64, On: input.raml)
SYStringNode: "type" (Start: 52 , End: 56, On: input.raml)
NativeTypeExpressionNode: "object" (Start: 58 , End: 64, On: input.raml, Source: SYStringNode)
FacetNode (Start: 69 , End: 144, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "properties" (Start: 69 , End: 79, On: input.raml)
SYObjectNode (Start: 87 , End: 144, On: input.raml)
PropertyNode (Start: 87 , End: 102, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "count?" (Start: 87 , End: 93, On: input.raml)
TypeDeclarationNode (Start: 95 , End: 102, On: input.raml, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: 102)
StringNodeImpl: "type" (Start: -1 , End: -1)
NativeTypeExpressionNode: "integer" (Start: 95 , End: 102, On: input.raml, Source: NativeTypeExpressionNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "count?" (Start: -1 , End: -1, Source: SYStringNode)
PropertyNode (Start: 109 , End: 123, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "flag?" (Start: 109 , End: 114, On: input.raml)
TypeDeclarationNode (Start: 116 , End: 123, On: input.raml, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: 123)
StringNodeImpl: "type" (Start: -1 , End: -1)
NativeTypeExpressionNode: "boolean" (Start: 116 , End: 123, On: input.raml, Source: NativeTypeExpressionNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "flag?" (Start: -1 , End: -1, Source: SYStringNode)
PropertyNode (Start: 130 , End: 143, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "name?" (Start: 130 , End: 135, On: input.raml)
TypeDeclarationNode (Start: 137 , End: 143, On: input.raml, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: 143)
StringNodeImpl: "type" (Start: -1 , End: -1)
NativeTypeExpressionNode: "string" (Start: 137 , End: 143, On: input.raml, Source: NativeTypeExpressionNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "name?" (Start: -1 , End: -1, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "TestType" (Start: -1 , End: -1, Source: SYStringNode)
ResourceNode (Start: 144 , End: 291, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "/test" (Start: 144 , End: 149, On: input.raml)
SYObjectNode (Start: 153 , End: 291, On: input.raml)
MethodNode (Start: 153 , End: 291, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "post" (Start: 153 , End: 157, On: input.raml)
SYObjectNode (Start: 163 , End: 291, On: input.raml)
BodyNode (Start: 163 , End: 291, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "body" (Start: 163 , End: 167, On: input.raml)
SYObjectNode (Start: 175 , End: 291, On: input.raml)
KeyValueNodeImpl (Start: 175 , End: 291, On: input.raml)
SYStringNode: "application/json" (Start: 175 , End: 191, On: input.raml)
TypeDeclarationNode (Start: 201 , End: 291, On: input.raml, Source: SYObjectNode)
KeyValueNodeImpl (Start: 201 , End: 215, On: input.raml)
SYStringNode: "type" (Start: 201 , End: 205, On: input.raml)
NamedTypeExpressionNode TestType -> {TypeDeclarationNode RefStart: 52 , RefEnd: 144} (Start: 207 , End: 215, On: input.raml, Source: SYStringNode)
ExampleDeclarationNode (Start: 224 , End: 291, On: input.raml, Source: KeyValueNodeImpl)
SYStringNode: "example" (Start: 224 , End: 231, On: input.raml)
SYStringNode: "{ "count": 12, "flag": "true", "name": "Joe" }" (Start: 233 , End: 291, On: input.raml)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "application/json" (Start: -1 , End: -1, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "post" (Start: -1 , End: -1, Source: SYStringNode)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "displayName" (Start: -1 , End: -1)
ObjectNodeImpl (Start: -1 , End: -1)
KeyValueNodeImpl (Start: -1 , End: -1)
StringNodeImpl: "value" (Start: -1 , End: -1)
OverlayableStringNode: "/test" (Start: -1 , End: -1, Source: SYStringNode)
2 changes: 1 addition & 1 deletion yagi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.raml</groupId>
<artifactId>raml-parser-parent</artifactId>
<version>1.0.46-SNAPSHOT</version>
<version>1.0.48-SNAPSHOT</version>
</parent>
<artifactId>yagi</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,23 @@

public class BooleanTypeRule extends AbstractTypeRule
{
private static final String STRICT_BOOLEANS = "org.raml.strict_booleans";

private final static String TRUE = "true";
private final static String FALSE = "false";

private final boolean strictBoolean;

public BooleanTypeRule()
{
this.strictBoolean = Boolean.parseBoolean(System.getProperty(STRICT_BOOLEANS, "false"));
}

BooleanTypeRule(boolean strictBoolean)
{
this.strictBoolean = strictBoolean;
}

@Nonnull
@Override
public List<Suggestion> getSuggestions(Node node, ParsingContext context)
Expand All @@ -42,7 +56,7 @@ public List<Suggestion> getSuggestions(Node node, ParsingContext context)
@Override
public boolean matches(@Nonnull Node node)
{
if (node instanceof StringNode)
if (!strictBoolean && (node instanceof StringNode))
{
String value = ((StringNode) node).getValue();
return TRUE.equals(value) || FALSE.equals(value);
Expand Down

0 comments on commit 9f0fc1b

Please sign in to comment.