madlisp/test/UtilTest.php

102 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2020-12-17 02:45:37 +00:00
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Env;
2020-12-17 02:45:37 +00:00
use MadLisp\Hash;
use MadLisp\MadLispException;
use MadLisp\Symbol;
2020-12-17 02:45:37 +00:00
use MadLisp\Util;
use MadLisp\Vector;
2020-12-17 02:45:37 +00:00
class UtilTest extends TestCase
{
public function testBindArguments()
{
$env = new Env('env');
Util::bindArguments($env, [
new Symbol('a'),
new Symbol('b')
], [
1,
2
]);
$this->assertSame(['a' => 1, 'b' => 2], $env->getData());
}
public function testBindArgumentsVariable()
{
$env = new Env('env');
Util::bindArguments($env, [
new Symbol('a'),
new Symbol('b'),
new Symbol('&'),
new Symbol('c')
], [
1,
2,
3,
4
]);
$data = $env->getData();
$this->assertCount(3, $data);
$this->assertSame(1, $data['a']);
$this->assertSame(2, $data['b']);
$vec = $data['c'];
$this->assertInstanceOf(Vector::class, $vec);
$this->assertSame([3, 4], $vec->getData());
}
public function testBindArgumentsVariableInvalid()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('no binding after &');
$env = new Env('env');
Util::bindArguments($env, [
new Symbol('a'),
new Symbol('b'),
new Symbol('&')
], [
1,
2,
3,
4
]);
}
2020-12-17 02:45:37 +00:00
public function testMakeHash()
{
$hash = Util::makeHash(['a', 1, 'b', 2]);
$this->assertInstanceOf(Hash::class, $hash);
$this->assertSame(['a' => 1, 'b' => 2], $hash->getData());
}
public function testMakeHashUnevenArgs()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('uneven number of arguments for hash');
Util::makeHash(['a', 1, 'b', 2, 'c']);
}
public function testMakeHashInvalidKey()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('invalid key for hash (not string)');
Util::makeHash([1, 2]);
}
}