Skip to content

Commit

Permalink
Add unit tests for all matchers
Browse files Browse the repository at this point in the history
Make NullMatcher a static field on ValueMatcher.
  • Loading branch information
Mats-SX committed Mar 3, 2016
1 parent 342cdf0 commit 762b10f
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 41 deletions.
Expand Up @@ -27,19 +27,16 @@
import cypher.feature.parser.matchers.ListMatcher;
import cypher.feature.parser.matchers.MapMatcher;
import cypher.feature.parser.matchers.NodeMatcher;
import cypher.feature.parser.matchers.NullMatcher;
import cypher.feature.parser.matchers.StringMatcher;
import cypher.feature.parser.matchers.ValueMatcher;

import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;

class CypherMatchersCreator extends FeatureResultsBaseListener
Expand Down Expand Up @@ -75,7 +72,7 @@ public void enterInteger( FeatureResultsParser.IntegerContext ctx )
@Override
public void enterNullValue( FeatureResultsParser.NullValueContext ctx )
{
workload.push( new NullMatcher() );
workload.push( ValueMatcher.NULL_MATCHER );
}

@Override
Expand Down
Expand Up @@ -33,4 +33,10 @@ public boolean matches( Object value )
{
return value instanceof Boolean && (Boolean) value == this.value;
}

@Override
public String toString()
{
return "BooleanMatcher for " + value;
}
}
Expand Up @@ -33,4 +33,10 @@ public boolean matches( Object value )
{
return value instanceof Double && value.equals( this.value );
}

@Override
public String toString()
{
return "FloatMatcher for " + value;
}
}
Expand Up @@ -33,4 +33,10 @@ public boolean matches( Object value )
{
return value instanceof Long && (Long) value == this.value;
}

@Override
public String toString()
{
return "IntegerMatcher for " + value;
}
}
Expand Up @@ -37,12 +37,23 @@ public boolean matches( Object value )
{
List realList = (List) value;
boolean match = realList.size() == list.size();
for ( int i = 0; i < list.size(); ++i )
if ( match )
{
match &= list.get( i ).matches( realList.get( i ) );
for ( int i = 0; i < list.size(); ++i )
{
ValueMatcher valueMatcher = list.get( i );
Object value1 = realList.get( i );
match &= valueMatcher.matches( value1 );
}
}
return match;
}
return false;
}

@Override
public String toString()
{
return "ListMatcher for " + list.toString();
}
}
Expand Up @@ -25,13 +25,7 @@

public class MapMatcher implements ValueMatcher
{
public static final MapMatcher EMPTY = new MapMatcher( Collections.emptyMap() ) {
@Override
public String toString()
{
return "{ MapMatcher matching the empty map }";
}
};
public static final MapMatcher EMPTY = new MapMatcher( Collections.emptyMap() );

private final Map<String,ValueMatcher> map;

Expand Down Expand Up @@ -65,6 +59,6 @@ public boolean matches( Object value )
@Override
public String toString()
{
return "{ MapMatcher matching a map with keys " + map.keySet().toString() + " and values " + map.values().toString() + " }";
return "MapMatcher for " + map.toString();
}
}
Expand Up @@ -21,16 +21,22 @@

public class StringMatcher implements ValueMatcher
{
private final String value;
private final String string;

public StringMatcher( String value )
public StringMatcher( String string )
{
this.value = value;
this.string = string;
}

@Override
public boolean matches( Object value )
{
return value instanceof String && value.toString().equals( value );
return value instanceof String && value.toString().equals( string );
}

@Override
public String toString()
{
return "StringMatcher for " + string;
}
}
Expand Up @@ -22,4 +22,19 @@
public interface ValueMatcher
{
boolean matches( Object value );

ValueMatcher NULL_MATCHER = new ValueMatcher()
{
@Override
public boolean matches( Object value )
{
return value == null;
}

@Override
public String toString()
{
return "NullMatcher";
}
};
}
@@ -0,0 +1,13 @@
package cypher.feature.parser

import cypher.feature.parser.matchers.ValueMatcher
import org.scalatest.matchers.{MatchResult, Matcher}

case class accept(value: Any) extends Matcher[ValueMatcher] {

override def apply(matcher: ValueMatcher): MatchResult = {
MatchResult(matches = matcher.matches(value),
s"$matcher did not match $value",
s"$matcher unexpectedly matched $value")
}
}
Expand Up @@ -21,14 +21,11 @@ package cypher.feature.parser

import java.lang.Boolean.{FALSE, TRUE}
import java.lang.Long
import java.util.Arrays.asList
import java.util.Collections.{emptyList, emptyMap}
import java.util.Collections.emptyList
import java.{lang, util}

import org.mockito.Mockito._
import org.mockito.Mockito.{when, mock, _}

import cypher.feature.parser.matchers.ValueMatcher
import org.mockito.Mockito.{mock, when}
import org.neo4j.graphdb._
import org.scalatest.matchers.{MatchResult, Matcher}
import org.scalatest.{FunSuite, Matchers}
Expand Down Expand Up @@ -208,14 +205,4 @@ class expectedResultsParserTest extends FunSuite with Matchers {
map
}

case class accept(expected: Any)
extends Matcher[ValueMatcher] {

override def apply(actual: ValueMatcher): MatchResult = {
MatchResult(matches = actual.matches(expected),
s"Mismatch! Expected value $actual did not match $expected",
s"No mismatch found; $actual unexpectedly matched $expected")
}
}

}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cypher.feature.parser.matchers

import cypher.feature.parser.accept
import org.scalatest.FunSuite
import org.scalatest.Matchers._

class BooleanMatcherTest extends FunSuite {

test("should match true") {
new BooleanMatcher(true) should accept(true)
}

test("should match false") {
new BooleanMatcher(false) should accept(false)
}

test("should not match other values") {
new BooleanMatcher(true) shouldNot accept(false)
new BooleanMatcher(true) shouldNot accept(null)
new BooleanMatcher(true) shouldNot accept("")

new BooleanMatcher(false) shouldNot accept(true)
new BooleanMatcher(false) shouldNot accept(null)
new BooleanMatcher(false) shouldNot accept("")
}

}
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cypher.feature.parser.matchers

import cypher.feature.parser.accept
import org.scalatest.FunSuite
import org.scalatest.Matchers._

class FloatMatcherTest extends FunSuite {

test("should match simple float") {
new FloatMatcher(1.0) should accept(1.0)
new FloatMatcher(.1e1) should accept(1.0)
}

test("should match infinities") {
new FloatMatcher(Double.PositiveInfinity) should accept(Double.PositiveInfinity)
new FloatMatcher(Double.NegativeInfinity) should accept(Double.NegativeInfinity)
}

test("should match NaN") {
new FloatMatcher(Double.NaN) should accept(Double.NaN)
}

test("should not match integers") {
new FloatMatcher(1.0) shouldNot accept(1)
new FloatMatcher(0.0) shouldNot accept(0)
}

test("should not match strings") {
new FloatMatcher(0.0) shouldNot accept("0.0")
new FloatMatcher(0.0e1) shouldNot accept("0")
}

}
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cypher.feature.parser.matchers

import cypher.feature.parser.accept
import org.scalatest.FunSuite
import org.scalatest.Matchers._

class IntegerMatcherTest extends FunSuite {

test("should match integers") {
new IntegerMatcher(0) should accept(0L)
new IntegerMatcher(-1) should accept(-1L)
new IntegerMatcher(2) should accept(2L)
}

test("should only match longs") {
new IntegerMatcher(0) shouldNot accept(0)
new IntegerMatcher(0) shouldNot accept(0.asInstanceOf[Short])
}

test("should not accept other types") {
new IntegerMatcher(0) shouldNot accept(null)
new IntegerMatcher(0) shouldNot accept("0")
new IntegerMatcher(0) shouldNot accept(0.0)
}

test("should match large ints") {
new IntegerMatcher(Long.MaxValue) should accept(Long.MaxValue)
}

}

0 comments on commit 762b10f

Please sign in to comment.