"""Tests for core.parameter_parser module.""" import pytest from core.parameter_parser import ( parse, parse_int, parse_float, parse_string, parse_combo, parse_seed, ) class TestParseInt: def test_comma_separated(self): assert parse_int("10, 20, 30") == [10, 20, 30] def test_single_value(self): assert parse_int("42") == [42] def test_range_with_step(self): assert parse_int("10-30, 5") == [10, 15, 20, 25, 30] def test_range_without_step(self): assert parse_int("1-5") == [1, 2, 3, 4, 5] def test_range_descending(self): assert parse_int("30-10, 5") == [30, 25, 20, 15, 10] def test_negative_values(self): assert parse_int("-5, 0, 5") == [-5, 0, 5] def test_negative_range_start(self): assert parse_int("-2-2") == [-2, -1, 0, 1, 2] def test_whitespace_handling(self): assert parse_int(" 10 , 20 , 30 ") == [10, 20, 30] def test_invalid_value(self): with pytest.raises(ValueError, match="Cannot parse"): parse_int("10, abc, 30") def test_zero_step(self): with pytest.raises(ValueError, match="Step must be positive"): parse_int("10-20, 0") def test_same_start_end(self): assert parse_int("5-5") == [5] class TestParseFloat: def test_comma_separated(self): assert parse_float("0.5, 1.0, 1.5") == [0.5, 1.0, 1.5] def test_single_value(self): assert parse_float("7.5") == [7.5] def test_range_with_step(self): result = parse_float("0.5-1.5, 0.25") assert result == [0.5, 0.75, 1.0, 1.25, 1.5] def test_range_without_step(self): assert parse_float("1-3") == [1.0, 2.0, 3.0] def test_range_descending(self): result = parse_float("1.5-0.5, 0.5") assert result == [1.5, 1.0, 0.5] def test_precision(self): """Verify no floating-point drift in range generation.""" result = parse_float("0.1-0.3, 0.1") assert len(result) == 3 assert result[0] == 0.1 assert result[1] == 0.2 assert result[2] == 0.3 def test_invalid_value(self): with pytest.raises(ValueError, match="Cannot parse"): parse_float("0.5, abc, 1.5") class TestParseString: def test_newline_separated(self): assert parse_string("cat\ndog\nbird") == ["cat", "dog", "bird"] def test_single_value(self): assert parse_string("hello world") == ["hello world"] def test_strips_whitespace(self): assert parse_string(" cat \n dog \n bird ") == [ "cat", "dog", "bird", ] def test_skips_empty_lines(self): assert parse_string("cat\n\ndog\n\nbird") == ["cat", "dog", "bird"] def test_multiline_prompts(self): text = "a beautiful sunset\na majestic mountain\na calm ocean" result = parse_string(text) assert len(result) == 3 assert result[0] == "a beautiful sunset" class TestParseCombo: def test_comma_separated(self): assert parse_combo("euler, dpm_2, ddim") == ["euler", "dpm_2", "ddim"] def test_single_value(self): assert parse_combo("euler") == ["euler"] def test_strips_whitespace(self): assert parse_combo(" euler , dpm_2 ") == ["euler", "dpm_2"] def test_skips_empty(self): assert parse_combo("euler,,dpm_2") == ["euler", "dpm_2"] class TestParseSeed: def test_comma_separated(self): assert parse_seed("42, 123, 999") == [42, 123, 999] def test_random_generates_correct_count(self): result = parse_seed("random:5") assert len(result) == 5 assert all(isinstance(s, int) for s in result) def test_random_case_insensitive(self): result = parse_seed("Random:3") assert len(result) == 3 def test_increment(self): assert parse_seed("increment:100:5") == [100, 101, 102, 103, 104] def test_random_zero_count(self): with pytest.raises(ValueError, match="positive"): parse_seed("random:0") def test_random_too_large(self): with pytest.raises(ValueError, match="too large"): parse_seed("random:100000") class TestParseDispatch: def test_int_dispatch(self): assert parse("10, 20, 30", "INT") == [10, 20, 30] def test_float_dispatch(self): assert parse("0.5, 1.0", "FLOAT") == [0.5, 1.0] def test_string_dispatch(self): assert parse("cat\ndog", "STRING") == ["cat", "dog"] def test_combo_dispatch(self): assert parse("euler, ddim", "COMBO") == ["euler", "ddim"] def test_seed_dispatch(self): assert parse("42, 123", "SEED") == [42, 123] def test_unknown_type(self): with pytest.raises(ValueError, match="Unknown parameter type"): parse("test", "UNKNOWN") def test_empty_string(self): assert parse("", "INT") == [] def test_whitespace_only(self): assert parse(" ", "FLOAT") == [] def test_case_insensitive_type(self): assert parse("10, 20", "int") == [10, 20]