Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Sep 10, 2023
1 parent 70f7fd1 commit 2127db4
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class SpecialForms_VarFunctions {
" (def x 10) \n" +
" (var-get 'x))")
.seeAlso(
"var-ns", "var-name", "var-local?", "var-global?", "var-thread-local?")
"var-name", "var-ns", "var-local?", "var-global?", "var-thread-local?")
.build()
) {
@Override
Expand All @@ -79,6 +79,7 @@ public VncVal apply(
) {
specialFormCallValidation(ctx, "var-get");
assertArity("var-get", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
Expand All @@ -89,6 +90,62 @@ public VncVal apply(
private static final long serialVersionUID = -1848883965231344442L;
};

public static VncSpecialForm var_name =
new VncSpecialForm(
"var-name",
VncSpecialForm
.meta()
.arglists("(var-name v)")
.doc("Returns the name of the var's symbol")
.examples(
"(var-name +)",
"(var-name '+)",
"(var-name (symbol \"+\"))",
";; aliased function \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (var-name add))",
"(do \n" +
" (def x 10) \n" +
" (var-name x))",
"(let [x 10] \n" +
" (var-name x))",
";; compare with name \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (name add))",
";; compare aliased function with name \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (name add))")
.seeAlso(
"name", "var-get", "var-ns", "var-local?", "var-global?", "var-thread-local?")
.build()
) {
@Override
public VncVal apply(
final VncVal specialFormMeta,
final VncList args,
final Env env,
final SpecialFormsContext ctx
) {
specialFormCallValidation(ctx, "var-name");
assertArity("var-name", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
ctx.getEvaluator().evaluate(args.first(), env, false));
return new VncString(sym.getSimpleName());
}

private static final long serialVersionUID = -1848883965231344442L;

};

public static VncSpecialForm var_ns =
new VncSpecialForm(
"var-ns",
Expand Down Expand Up @@ -159,39 +216,19 @@ else if (env.isLocal(sym)) {
private static final long serialVersionUID = -1848883965231344442L;
};

public static VncSpecialForm var_name =
public static VncSpecialForm var_sym =
new VncSpecialForm(
"var-name",
"var-sym",
VncSpecialForm
.meta()
.arglists("(var-name v)")
.doc("Returns the name of the var's symbol")
.arglists("(var-sym v)")
.doc("Returns the var's symbol")
.examples(
"(var-name +)",
"(var-name '+)",
"(var-name (symbol \"+\"))",
";; aliased function \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (var-name add))",
"(do \n" +
" (def x 10) \n" +
" (var-name x))",
"(let [x 10] \n" +
" (var-name x))",
";; compare with name \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (name add))",
";; compare aliased function with name \n" +
"(do \n" +
" (ns foo) \n" +
" (def add +)\n" +
" (name add))")
"(var-sym +)",
"(var-sym '+)",
"(var-sym (symbol \"+\"))")
.seeAlso(
"name", "var-get", "var-ns", "var-local?", "var-global?", "var-thread-local?")
"name", "var-get", "var-name", "var-ns", "var-local?", "var-global?", "var-thread-local?")
.build()
) {
@Override
Expand All @@ -201,13 +238,14 @@ public VncVal apply(
final Env env,
final SpecialFormsContext ctx
) {
specialFormCallValidation(ctx, "var-name");
assertArity("var-name", FnType.SpecialForm, args, 1);
specialFormCallValidation(ctx, "var-sym");
assertArity("var-sym", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
ctx.getEvaluator().evaluate(args.first(), env, false));
return new VncString(sym.getSimpleName());
return env.getSymbol(sym);
}

private static final long serialVersionUID = -1848883965231344442L;
Expand All @@ -225,13 +263,13 @@ public VncVal apply(
"(var-local? +)",
"(var-local? '+)",
"(var-local? (symbol \"+\"))",
"(let [x 10] \n" +
" (var-local? x)) ",
"(do \n" +
" (def x 10) \n" +
" (var-local? x)) ",
"(let [x 10] \n" +
" (var-local? x)) ")
.seeAlso(
"var-get", "var-ns", "var-name", "var-global?", "var-thread-local?")
"var-get", "var-name", "var-ns", "var-global?", "var-thread-local?")
.build()
) {
@Override
Expand All @@ -242,6 +280,7 @@ public VncVal apply(
final SpecialFormsContext ctx
) {
assertArity("var-local?", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
Expand All @@ -261,9 +300,9 @@ public VncVal apply(
.doc("Returns true if the var is thread-local else false")
.examples(
"(binding [x 100] \n" +
" (var-local? x))")
" (var-thread-local? x))")
.seeAlso(
"var-get", "var-ns", "var-name", "var-local?", "var-global?")
"var-get", "var-name", "var-ns", "var-local?", "var-global?")
.build()
) {
@Override
Expand All @@ -274,6 +313,7 @@ public VncVal apply(
final SpecialFormsContext ctx
) {
assertArity("var-thread-local?", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
Expand Down Expand Up @@ -301,7 +341,7 @@ public VncVal apply(
"(let [x 10] \n" +
" (var-global? x)) ")
.seeAlso(
"var-get", "var-ns", "var-name", "var-local?", "var-thread-local?")
"var-get", "var-name", "var-ns", "var-local?", "var-thread-local?")
.build()
) {
@Override
Expand All @@ -312,6 +352,7 @@ public VncVal apply(
final SpecialFormsContext ctx
) {
assertArity("var-global?", FnType.SpecialForm, args, 1);

final VncSymbol sym = Types.isVncSymbol(args.first())
? (VncSymbol)args.first()
: Coerce.toVncSymbol(
Expand All @@ -332,10 +373,11 @@ public VncVal apply(
public static final Map<VncVal, VncVal> ns =
new SymbolMapBuilder()
.add(var_get)
.add(var_globalQ)
.add(var_localQ)
.add(var_name)
.add(var_sym)
.add(var_ns)
.add(var_globalQ)
.add(var_localQ)
.add(var_thread_localQ)
.toMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package com.github.jlangch.venice.impl.specialforms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

Expand All @@ -45,4 +47,29 @@ public void test_var_name() {
assertEquals("split", new Venice().eval("(var-name str/split)"));
assertEquals("x", new Venice().eval("(let [x 100] (var-name x))"));
}

@Test
public void test_var_global() {
assertTrue( (Boolean)new Venice().eval("(do (def x 100) (var-global? x))"));
assertFalse((Boolean)new Venice().eval("(do (def x 100) (var-local? x))"));
assertFalse((Boolean)new Venice().eval("(do (def x 100) (var-thread-local? x))"));
}

@Test
public void test_var_local() {
assertFalse((Boolean)new Venice().eval("(do (let [x 100] (var-global? x)))"));
assertTrue( (Boolean)new Venice().eval("(do (let [x 100] (var-local? x)))"));
assertFalse((Boolean)new Venice().eval("(do (let [x 100] (var-thread-local? x)))"));

assertFalse((Boolean)new Venice().eval("(do (defn foo [x] (var-global? x)) (foo 0))"));
assertTrue( (Boolean)new Venice().eval("(do (defn foo [x] (var-local? x)) (foo 0))"));
assertFalse((Boolean)new Venice().eval("(do (defn foo [x] (var-thread-local? x)) (foo 0))"));
}

@Test
public void test_var_threadlocal() {
assertFalse((Boolean)new Venice().eval("(do (binding [x 100] (var-global? x)))"));
assertFalse((Boolean)new Venice().eval("(do (binding [x 100] (var-local? x)))"));
assertTrue( (Boolean)new Venice().eval("(do (binding [x 100] (var-thread-local? x)))"));
}
}

0 comments on commit 2127db4

Please sign in to comment.