From 45b45919c1ca215948beee8d17e7ac24093d082c Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 12 Mar 2024 12:14:36 +0100 Subject: [PATCH] Add TestGetCharRangeFromConfig function in random_test.go A comprehensive unit test, TestGetCharRangeFromConfig, has been added to the random_test.go file. This test validates the GetCharRangeFromConfig function across various scenarios and configurations. It also improves code reliability and makes the application more robust against potential issues. --- random_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/random_test.go b/random_test.go index a7fc479..d49776d 100644 --- a/random_test.go +++ b/random_test.go @@ -147,6 +147,76 @@ func TestGenerator_RandomString(t *testing.T) { } } +func TestGetCharRangeFromConfig(t *testing.T) { + // Arrange + config := NewConfig() + generator := New(config) + + // Test cases + testCases := []struct { + Name string + ConfigMode ModeMask + ExpectedRange string + }{ + { + Name: "LowerCaseHumanReadable", + ConfigMode: ModeLowerCase | ModeHumanReadable, + ExpectedRange: CharRangeAlphaLowerHuman, + }, + { + Name: "LowerCaseNonHumanReadable", + ConfigMode: ModeLowerCase, + ExpectedRange: CharRangeAlphaLower, + }, + { + Name: "NumericHumanReadable", + ConfigMode: ModeNumeric | ModeHumanReadable, + ExpectedRange: CharRangeNumericHuman, + }, + { + Name: "NumericNonHumanReadable", + ConfigMode: ModeNumeric, + ExpectedRange: CharRangeNumeric, + }, + { + Name: "SpecialHumanReadable", + ConfigMode: ModeSpecial | ModeHumanReadable, + ExpectedRange: CharRangeSpecialHuman, + }, + { + Name: "SpecialNonHumanReadable", + ConfigMode: ModeSpecial, + ExpectedRange: CharRangeSpecial, + }, + { + Name: "UpperCaseHumanReadable", + ConfigMode: ModeUpperCase | ModeHumanReadable, + ExpectedRange: CharRangeAlphaUpperHuman, + }, + { + Name: "UpperCaseNonHumanReadable", + ConfigMode: ModeUpperCase, + ExpectedRange: CharRangeAlphaUpper, + }, + { + Name: "MultipleModes", + ConfigMode: ModeLowerCase | ModeNumeric | ModeUpperCase | ModeHumanReadable, + ExpectedRange: CharRangeAlphaLowerHuman + CharRangeNumericHuman + CharRangeAlphaUpperHuman, + }, + } + + // Act and assert for each test case + for _, tc := range testCases { + t.Run(tc.Name, func(t *testing.T) { + generator.config.Mode = tc.ConfigMode + actualRange := generator.GetCharRangeFromConfig() + if actualRange != tc.ExpectedRange { + t.Errorf("Expected range %s, got %s", tc.ExpectedRange, actualRange) + } + }) + } +} + func BenchmarkGenerator_CoinFlip(b *testing.B) { b.ReportAllocs() g := New(NewConfig())