Package gololang

Examples of gololang.GoloStruct


    Class<?> moduleClass = compileAndLoadGoloModule(SRC, "structs-outside.golo", goloClassLoader);

    Method smoke_test = moduleClass.getMethod("smoke_test");
    Object result = smoke_test.invoke(null);
    assertThat(result, instanceOf(GoloStruct.class));
    GoloStruct struct = (GoloStruct) result;
    assertThat(struct.get("name"), is((Object) "foo"));
    assertThat(struct.get("email"), is((Object) "bar"));

    Method bam = moduleClass.getMethod("bam");
    try {
      bam.invoke(null);
      fail("An InvocationTargetException was expected");
View Full Code Here


    assertThat((String) result, is("Mr Bean <mrbean@outlook.com>"));

    Method mrbean_struct = moduleClass.getMethod("mrbean_struct");
    result = mrbean_struct.invoke(null);
    assertThat(result, instanceOf(GoloStruct.class));
    GoloStruct struct = (GoloStruct) result;

    Tuple tuple = struct.members();
    assertThat(tuple.size(), is(2));
    assertThat(tuple.get(0), is((Object) "name"));
    assertThat(tuple.get(1), is((Object) "email"));

    tuple = struct.values();
    assertThat(tuple.size(), is(2));
    assertThat(tuple.get(0), is((Object) "Mr Bean"));
    assertThat(tuple.get(1), is((Object) "mrbean@outlook.com"));

    Iterator<Tuple> structIterator = struct.iterator();
    assertThat(structIterator.hasNext(), is(true));
    tuple = structIterator.next();
    assertThat(tuple.size(), is(2));
    assertThat(tuple.get(0), is((Object) "name"));
    assertThat(tuple.get(1), is((Object) "Mr Bean"));
    assertThat(structIterator.hasNext(), is(true));
    tuple = structIterator.next();
    assertThat(tuple.size(), is(2));
    assertThat(tuple.get(0), is((Object) "email"));
    assertThat(tuple.get(1), is((Object) "mrbean@outlook.com"));
    assertThat(structIterator.hasNext(), is(false));

    assertThat(struct.get("name"), is((Object) "Mr Bean"));
    assertThat(struct.get("email"), is((Object) "mrbean@outlook.com"));
    try {
      struct.get("foo");
      fail("An IllegalArgumentException was expected");
    } catch (IllegalArgumentException ignored) {
    }

    struct = struct.copy();
    struct.set("name", "John");
    assertThat(struct.get("name"), is((Object) "John"));
    try {
      struct.set("foo", "bar");
      fail("An IllegalArgumentException was expected");
    } catch (IllegalArgumentException ignored) {
    }

    assertThat(struct.isFrozen(), is(false));
    struct = struct.frozenCopy();
    assertThat(struct.isFrozen(), is(true));
    assertThat(struct.copy().isFrozen(), is(false));
    try {
      struct.set("name", "John");
      fail("An IllegalStateException was expected");
    } catch (IllegalStateException ignored) {
    }

    Method mrbean_toString = moduleClass.getMethod("mrbean_toString");
    result = mrbean_toString.invoke(null);
    assertThat(result, instanceOf(String.class));
    assertThat((String) result, is("struct Contact{name=Mr Bean, email=mrbean@outlook.com}"));

    Method mrbean_copy = moduleClass.getMethod("mrbean_copy");
    result = mrbean_copy.invoke(null);
    assertThat(result, instanceOf(Tuple.class));
    tuple = (Tuple) result;
    assertThat(tuple.get(0).toString(), is("struct Contact{name=Mr Bean, email=mrbean@outlook.com}"));
    assertThat(tuple.get(1).toString(), is("struct Contact{name=Mr Bean, email=mrbean@outlook.com}"));
    assertThat(tuple.get(0), not(sameInstance(tuple.get(1))));

    Method mrbean_frozenCopy = moduleClass.getMethod("mrbean_frozenCopy");
    result = mrbean_frozenCopy.invoke(null);
    assertThat(result, instanceOf(Tuple.class));
    tuple = (Tuple) result;
    assertThat(tuple.get(0).toString(), is("struct Contact{name=Mr Bean, email=mrbean@outlook.com}"));
    assertThat(tuple.get(1).toString(), is("struct Contact{name=Mr Bean, email=mrbean@outlook.com}"));
    assertThat(tuple.get(0), not(sameInstance(tuple.get(1))));
    try {
      Object instance = tuple.get(1);
      Method name = instance.getClass().getMethod("name", Object.class);
      name.invoke(instance, "Foo");
      fail("A frozen struct shall not allow field mutation");
      name.invoke(tuple.get(0), "Foo");
    } catch (InvocationTargetException e) {
      if (!(e.getCause() instanceof IllegalStateException)) {
        throw e;
      }
    }

    Method mrbean_hashCode = moduleClass.getMethod("mrbean_hashCode");
    result = mrbean_hashCode.invoke(null);
    assertThat(result, instanceOf(Tuple.class));
    tuple = (Tuple) result;
    assertThat(tuple.get(0).hashCode(), not(tuple.get(1).hashCode()));
    assertThat(tuple.get(2).hashCode(), is(tuple.get(3).hashCode()));

    Method mrbean_equals = moduleClass.getMethod("mrbean_equals");
    result = mrbean_equals.invoke(null);
    assertThat(result, instanceOf(Tuple.class));
    tuple = (Tuple) result;
    assertThat(tuple.get(0), not(tuple.get(1)));
    assertThat(tuple.get(0), not(new Object()));
    assertThat(tuple.get(0), not(tuple.get(2)));
    assertThat(tuple.get(2), is(tuple.get(3)));
    assertThat(tuple.get(2), not(tuple.get(4)));
    assertThat(tuple.get(2), not(tuple.get(5)));
    assertThat(tuple.get(2), not(tuple.get(0)));

    Method immutable_factory = moduleClass.getMethod("immutable_factory");
    result = immutable_factory.invoke(null);
    assertThat(result, instanceOf(Tuple.class));
    tuple = (Tuple) result;
    assertThat(tuple.get(0), is(tuple.get(1)));

    Method fun_foo_bar_baz = moduleClass.getMethod("fun_foo_bar_baz");
    result = fun_foo_bar_baz.invoke(null);
    assertThat(result, instanceOf(GoloStruct.class));
    struct = (GoloStruct) result;
    assertThat(struct.members().size(), is(2));
    assertThat(struct.values().size(), is(2));
    structIterator = struct.iterator();
    assertThat(structIterator.hasNext(), is(true));
    assertThat(structIterator.next(), is(new Tuple("foo", 1)));
    assertThat(structIterator.hasNext(), is(true));
    assertThat(structIterator.next(), is(new Tuple("baz", 3)));
    assertThat(structIterator.hasNext(), is(false));
    assertThat(struct.get("foo"), is((Object) 1));
    try {
      struct.get("_bar");
      fail("An IllegalArgumentException was expected");
    } catch (IllegalArgumentException expected) {
    }
    assertThat(struct.copy().members().size(), is(2));

    Method augmented_foo_bar_baz = moduleClass.getMethod("augmented_foo_bar_baz");
    result = augmented_foo_bar_baz.invoke(null);
    assertThat(result, is((Object) 2));
  }
View Full Code Here

TOP

Related Classes of gololang.GoloStruct

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.