From f6f9daf4a118eadfb2bfce6487a18a8b1b2dd5de Mon Sep 17 00:00:00 2001 From: godcong Date: Thu, 11 Jul 2024 00:54:00 +0800 Subject: [PATCH] refactor(ident): revise test functions and add mock identifier Refactor the `ident` package test code to utilize a mock identifier implementation, which simplifies the testing of generation, validation, and size determination of identifiers. Removed redundant logging statements from the test functions and updated the tests to reflect the new mock-based approach. BREAKING CHANGE: Test functions now rely on a mock identifier that expects specific behavior. This may affect the way tests are set up and executed,requiring updates to the test suite to accommodate the changes. --- ident/ident_test.go | 64 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/ident/ident_test.go b/ident/ident_test.go index e3b6356..2f71af7 100644 --- a/ident/ident_test.go +++ b/ident/ident_test.go @@ -1,25 +1,59 @@ -// Copyright (c) 2024 OrigAdmin. All rights reserved. - -// Package ident provides the helpers functions. -package ident +package ident_test import ( - "strings" "testing" + + "github.com/origadmin/toolkits/ident" ) -func TestNewXID(t *testing.T) { - t.Logf("xid: %s", strings.ToUpper(NewXID())) - t.Logf("xid string size: %d", len(NewXID())) +type mockIdentifier struct{} + +func (m *mockIdentifier) Name() string { + return "MockIdentifier" +} + +func (m *mockIdentifier) Gen() string { + return "mock-generated-id" +} + +func (m *mockIdentifier) Validate(id string) bool { + return id == "mock-generated-id" } -func TestMustNewUUID(t *testing.T) { - t.Logf("uuid: %s", strings.ToUpper(MustNewUUID())) - t.Logf("uuid string size: %d", len(MustNewUUID())) +func (m *mockIdentifier) Size() int { + return 12 // Replace with the actual size of the generated identifier } -func TestMustNewULID(t *testing.T) { - t.Logf("ulid: %s", strings.ToUpper(MustNewULID())) - t.Logf("ulid string size: %d", len(MustNewULID())) - MustNewULID() +func TestGenID(t *testing.T) { + ident.Use(&mockIdentifier{}) + generatedID := ident.GenID() + expectedID := "mock-generated-id" + + if generatedID != expectedID { + t.Errorf("GenID() = %v, want %v", generatedID, expectedID) + } +} + +func TestGenSize(t *testing.T) { + ident.Use(&mockIdentifier{}) + size := ident.GenSize() + expectedSize := 12 // Replace with the actual size of the generated identifier + + if size != expectedSize { + t.Errorf("GenSize() = %v, want %v", size, expectedSize) + } +} + +func TestValidate(t *testing.T) { + ident.Use(&mockIdentifier{}) + validID := "mock-generated-id" + invalidID := "invalid-id" + + if !ident.Validate(validID) { + t.Errorf("Validate(%v) = %v, want %v", validID, false, true) + } + + if ident.Validate(invalidID) { + t.Errorf("Validate(%v) = %v, want %v", invalidID, true, false) + } }