Skip to content

Commit

Permalink
added tests for array/hash elements as lvalues; changed tests to use …
Browse files Browse the repository at this point in the history
…ShouldMatchers
  • Loading branch information
prakashk committed Apr 9, 2013
1 parent f5341b1 commit 8445b11
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
49 changes: 32 additions & 17 deletions src/test/scala/org/moe/parser/ArrayLiteralTestSuite.scala
Expand Up @@ -2,72 +2,87 @@ package org.moe.parser

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.ShouldMatchers

import org.moe.runtime._
import org.moe.interpreter._
import org.moe.ast._
import org.moe.parser._

class ArrayLiteralTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils {
class ArrayLiteralTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils with ShouldMatchers {

test("... basic test with a one-element arrayref") {
val result = interpretCode("[42]")

val listElement = result.unboxToArrayBuffer.get(0)
assert(listElement.unboxToInt.get === 42);
listElement.unboxToInt.get should equal (42);
}

test("... basic test with a four-element arrayref") {
val result = interpretCode("""[42, 'jason', "may", true]""")
val elems = result.unboxToArrayBuffer.get

assert(elems(0).unboxToInt.get === 42);
elems(0).unboxToInt.get should equal (42);

assert(elems(1).unboxToString.get === "jason");
assert(elems(2).unboxToString.get === "may");
assert(elems(3).unboxToBoolean.get === true);
elems(1).unboxToString.get should equal ("jason");
elems(2).unboxToString.get should equal ("may");
elems(3).unboxToBoolean.get should equal (true);
}

test("... basic test with a nested arrayref") {
val result = interpretCode("""[42, ['jason', "may"], true]""")
val elems = result.unboxToArrayBuffer.get

assert(elems(0).unboxToInt.get === 42);
elems(0).unboxToInt.get should equal (42);

val nested_elems = elems(1).unboxToArrayBuffer.get
assert(nested_elems(0).unboxToString.get === "jason");
assert(nested_elems(1).unboxToString.get === "may");
nested_elems(0).unboxToString.get should equal ("jason");
nested_elems(1).unboxToString.get should equal ("may");

assert(elems(2).unboxToBoolean.get === true);
elems(2).unboxToBoolean.get should equal (true);
}

test("... basic test with a nested hashref") {
val result = interpretCode("""[42, {'jason' => "may"}, false]""")
val elems = result.unboxToArrayBuffer.get

assert(elems(0).unboxToInt.get === 42);
elems(0).unboxToInt.get should equal (42);

val nested_hash = elems(1).unboxToMap.get
val key = nested_hash("jason")
assert(key.unboxToString.get === "may")
assert(elems(2).unboxToBoolean.get === false);
key.unboxToString.get should equal ("may")
elems(2).unboxToBoolean.get should equal (false);
}

test("... basic test with arrayref containing a right-trailing list") {
val result = interpretCode("[42, true, ]")
val elems = result.unboxToArrayBuffer.get

assert(elems(0).unboxToInt.get === 42);
assert(elems(1).unboxToBoolean.get === true);
elems(0).unboxToInt.get should equal (42);
elems(1).unboxToBoolean.get should equal (true);
}

test("... basic test with arrayref containing a left-trailing list") {
val result = interpretCode("[, 42, true]")
val elems = result.unboxToArrayBuffer.get

assert(elems(0).unboxToInt.get === 42);
assert(elems(1).unboxToBoolean.get === true);
elems(0).unboxToInt.get should equal (42);
elems(1).unboxToBoolean.get should equal (true);
}

test("... basic test for array element assignment") {
val result = interpretCode("my @a = [42, true]; @a[0] = @a[0] + 1; @a")
val elems = result.unboxToArrayBuffer.get

elems(0).unboxToInt.get should equal (43);
}

test("... basic test for array element assignment -- non-existent element") {
val result = interpretCode("""my @a = [42, true]; @a[3] = "foo"; @a""")
val elems = result.unboxToArrayBuffer.get

elems.length should equal (4)
elems(3).unboxToString.get should equal ("foo");
}

}
20 changes: 17 additions & 3 deletions src/test/scala/org/moe/parser/HashLiteralTestSuite.scala
Expand Up @@ -2,27 +2,41 @@ package org.moe.parser

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.ShouldMatchers

import org.moe.runtime._
import org.moe.interpreter._
import org.moe.ast._
import org.moe.parser._

class HashLiteralTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils {
class HashLiteralTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils with ShouldMatchers {

test("... basic test with hashref") {
val result = interpretCode("{foo => 'bar'}")
val map = result.unboxToMap.get
val key = map("foo")
assert(key.unboxToString.get === "bar")
key.unboxToString.get should equal ("bar")
}

test("... basic test with hashref of a string key") {
val result = interpretCode("{'foo' => 'bar'}")
val map = result.unboxToMap.get
val key = map("foo")
assert(key.unboxToString.get === "bar")
key.unboxToString.get should equal ("bar")
}

test("... basic test with hash element assignment") {
val result = interpretCode("my %h = {foo => 'bar'}; %h{'foo'} = 'baz'; %h")
val map = result.unboxToMap.get
val key = map("foo")
key.unboxToString.get should equal ("baz")
}

test("... basic test with hash element assignment -- non-existent element") {
val result = interpretCode("my %h = {foo => 'bar'}; %h{'baz'} = 'moe'; %h")
val map = result.unboxToMap.get
map("foo").unboxToString.get should equal ("bar")
map("baz").unboxToString.get should equal ("moe")
}

}

0 comments on commit 8445b11

Please sign in to comment.