Lambda calculus-like library written in TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
jlc/test/string.spec.ts

93 lines
2.5 KiB

import { describe, expect, it } from '@jest/globals';
import { L, _, undef } from '../src/core';
import { toBoolean as $b } from '../src/bool';
import { toChar as $c, fromChar as _c } from '../src/char';
import { toString as $s, Cons, Empty, String, fromString as _s, concat, eq } from '../src/string';
describe('String', () => {
const infinite: String = new L(() => Cons._(_c('a'))._(infinite).value); ;
describe('toString', () => {
it('converts ""', () => {
expect($s(Empty)).toBe('');
});
it('converts "a"', () => {
expect($s(Cons._(_c('a'))._(Empty))).toBe('a');
});
it('converts "abc"', () => {
expect($s(Cons._(_c('a'))._(Cons._(_c('b'))._(Cons._(_c('c'))._(Empty))))).toBe('abc');
});
});
describe('fromString', () => {
it('converts ""', () => {
expect($s(_s(''))).toBe('');
});
it('converts "a"', () => {
expect($s(_s('a'))).toBe('a');
});
it('converts "2"', () => {
expect($s(_s('abc'))).toBe('abc');
});
});
describe('concat', () => {
it('"" concat "" is ""', () => {
expect($s(concat._(Empty)._(Empty))).toBe('');
});
it('"a" concat "b" is "ab"', () => {
expect($s(concat._(_s('a'))._(_s('b')))).toBe('ab');
});
it('"abc" concat "def" is "abcdef"', () => {
expect($s(concat._(_s('abc'))._(_s('def')))).toBe('abcdef');
});
it('handles infinite strings', () => {
expect($c(concat._(infinite)._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe('a');
});
});
describe('eq', () => {
it('"" eq "" is True', () => {
expect($b(eq._(_s(''))._(_s('')))).toBe(true);
});
it('"" eq "abc" is False', () => {
expect($b(eq._(_s(''))._(_s('abc')))).toBe(false);
});
it('"abc" eq "" is False', () => {
expect($b(eq._(_s('abc'))._(_s('')))).toBe(false);
});
it('"a" eq "abc" is False', () => {
expect($b(eq._(_s('a'))._(_s('abc')))).toBe(false);
});
it('"abc" eq "a" is False', () => {
expect($b(eq._(_s('abc'))._(_s('a')))).toBe(false);
});
it('"bca" eq "abc" is False', () => {
expect($b(eq._(_s('bca'))._(_s('abc')))).toBe(false);
});
it('"abc" eq "bca" is False', () => {
expect($b(eq._(_s('abc'))._(_s('bca')))).toBe(false);
});
it('"abc" eq "abc" is True', () => {
expect($b(eq._(_s('abc'))._(_s('abc')))).toBe(true);
});
it('handles infinite strings', () => {
expect($b(eq._(_s('abc'))._(infinite))).toBe(false);
});
});
});