Livt packages

Eccelerators.Cli

Version 1.4.1

Transport-independent, bounded command-line input, parsing, editing, and output for Livt FPGA applications.

Published files (23)

tests/CliParserTest.lvt

namespace Eccelerators.Cli.Tests

using Eccelerators.Cli

@Test
component CliParserTest
{
	parser: CliParser

	new()
	{
		this.parser = new CliParser()
	}

	fn Feed(data: byte[])
	{
		for (var i = 0; i < data.Length(); i++) {
			this.parser.AppendByte(data[i])
		}
	}

	@Test
	fn ParsesCommandWithoutArguments()
	{
		this.parser.Clear()
		var line = "hello".Encode()
		this.Feed(line)
		this.parser.Parse()
		var expected = "hello".Encode()
		assert this.parser.CommandEquals(expected) == true
		assert this.parser.GetCommandLength() == 5
		assert this.parser.GetArgumentCount() == 0
	}

	@Test
	fn ParsesWhitespaceSeparatedArguments()
	{
		this.parser.Clear()
		var line = "  echo   one\ttwo  ".Encode()
		this.Feed(line)
		this.parser.Parse()
		var command = "echo".Encode()
		var first = "one".Encode()
		var second = "two".Encode()
		assert this.parser.CommandEquals(command) == true
		assert this.parser.GetArgumentCount() == 2
		assert this.parser.ArgumentEquals(0, first) == true
		assert this.parser.ArgumentEquals(1, second) == true
		assert this.parser.GetArgumentByte(1, 1) == 0x77
	}

	@Test
	fn RejectsPartialAndCaseDifferentCommands()
	{
		this.parser.Clear()
		var line = "Hello".Encode()
		this.Feed(line)
		this.parser.Parse()
		var lower = "hello".Encode()
		var partial = "Hell".Encode()
		assert this.parser.CommandEquals(lower) == false
		assert this.parser.CommandEquals(partial) == false
	}

	@Test
	fn ReportsTooManyArguments()
	{
		this.parser.Clear()
		var line = "c 0 1 2 3 4 5 6 7 8".Encode()
		this.Feed(line)
		this.parser.Parse()
		assert this.parser.GetArgumentCount() == this.parser.MAX_ARGUMENTS
		assert this.parser.HasTooManyArguments() == true
	}

	@Test
	fn RetainsTheCompleteRawLine()
	{
		this.parser.Clear()
		var line = "A little girl with an apple".Encode()
		this.Feed(line)
		this.parser.Parse()
		assert this.parser.GetLineLength() == line.Length()

		for (var index = 0; index < line.Length(); index++) {
			assert this.parser.GetLineByte(index) == line[index]
		}

		assert this.parser.GetLineByte(line.Length()) == 0x00
	}

	@Test
	fn HandlesEmptyLinesAndInvalidIndexes()
	{
		this.parser.Clear()
		this.parser.Parse()
		assert this.parser.GetCommandLength() == 0
		assert this.parser.GetArgumentCount() == 0
		assert this.parser.GetArgumentLength(0) == 0
		assert this.parser.GetArgumentByte(0, 0) == 0x00
	}

	@Test
	fn RejectsArgumentIndexOverflow()
	{
		this.parser.Clear()
		var line = "c abc".Encode()
		this.Feed(line)
		this.parser.Parse()
		assert this.parser.GetArgumentLength(0) == 3
		assert this.parser.GetArgumentByte(0, 2) == 0x63
		assert this.parser.GetArgumentLength(-1) == 0
		assert this.parser.GetArgumentLength(8) == 0
		assert this.parser.GetArgumentLength(2147483647) == 0
		assert this.parser.GetArgumentByte(-1, 0) == 0
		assert this.parser.GetArgumentByte(8, 0) == 0
		assert this.parser.GetArgumentByte(2147483647, 0) == 0
		assert this.parser.GetArgumentByte(0, -1) == 0
		assert this.parser.GetArgumentByte(0, 3) == 0
		assert this.parser.GetArgumentByte(0, 2147483647) == 0
	}
}