aboutsummaryrefslogtreecommitdiff
path: root/lib/test
AgeCommit message (Collapse)Author
3 dayslib/test: rewrite Assert to use [diff.Unified]Shulhan
Using diff Unified improve the test output readability when two large strings are not match.
10 daysall: apply go fixShulhan
2026-01-15all: convert license and copyright to use SPDX identifiersShulhan
With help of spdxconv tool [1], we able to bulk update all files license and copyright format to comply with SPDX formats. [1] https://kilabit.info/project/spdxconv/
2026-01-15lib/test: ignore line prefixed with "//"Shulhan
The first line in test data file may contains line prefixed with "//" as comment. The use case is for license and copyright lines.
2026-01-03lib/test: implement method ExtractInput on DataShulhan
Given the path to directory `destDir`, create all of the [test.Data.Input] with key as its file name. If the input name contains "/", the path before the base name will be created along with its parent as long as it is under the `destDir`. For example, given input name "a/b/c.txt", it will create path "a/b/" inside `destDir` first, followed by file "c.txt" inside that path.
2026-01-03lib/test: export the constant for default data file name suffixShulhan
Also, fix typo on the LoadDataDir regarding suffix by replacing it with the exported constant.
2025-02-04all: remove the nolint tagsShulhan
The "nolint" tag is used to ignore lines from being processed by golangci-lint. Since we are not using golangci-lint anymore, now and in the future, those lines can be removed.
2025-01-23all: use for-range with numericShulhan
Go 1.22 now support for-range on numeric value.
2025-01-23all: replace "interface{}" with "any"Shulhan
2025-01-22lib/test: call Helper method inside AssertShulhan
The Helper method mark the Assert function as test helper, which when printing file and line information, the stack trace from Assert function will be skipped. This remove manual lines skipping that previously we have.
2024-12-28lib/test: update comment to show link on reflect.EqualerShulhan
2024-03-14test/httptest: new helper for testing HTTP server handlerShulhan
The Simulate function simulate HTTP server handler by generating [http.Request] from fields in [SimulateRequest]; and then call [http.HandlerFunc]. The HTTP response from serve along with its raw body and original HTTP request then returned in [*SimulateResult].
2024-03-05all: comply with linter recommendations #2Shulhan
HTTP request now implicitly create request with context. Any false positive related to not closing HTTP response body has been annotated with "nolint:bodyclose". In the example code, use consistent "// Output:" comment format, by prefixing with single space. Any comment on code now also prefixing with single space. An error returned without variables now use [errors.New] instead of [fmt.Errorf]. Any error returned using [fmt.Errorf] now wrapped using "%w" instead of "%s". Also, replace error checking using [errors.Is] or [errors.As], instead of using equal/not-equal operator. Any statement like "x = x OP y" now replaced with "x OP= y". Also, swap statement is simplified using "x, y = y, x". Any switch statement with single case now replaced with if-condition. Any call to defer on function or program that call [os.Exit], now replaced by calling the deferred function directly. Any if-else condition now replaced with switch statement, if possible.
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2024-01-29test/mock: implement mock for crypto [rand.Reader]Shulhan
The RandReader implement [io.Reader]. To provide predictable result, the RandReader is seeded with slice of bytes. A call to Read will fill the passed bytes with those seed. For example, given seed as "abc" (length is three), calling Read with bytes length five will return "abcab".
2023-12-13all: fix linter warnings reported by reviveShulhan
There are some reports that I disagree with revive, in example, code should not declare the type after variables. In my opinion, on some cases, declaring the type make the code more readable and explicit. Since I did not want to add new configuration file, we changes it and follow revive for now.
2023-11-14lib/test: fix link to Article in DataShulhan
2023-09-11lib/test: realign struct in ExampleAssert_structShulhan
This reduce the struct size from 24 to 16 bytes (-8).
2023-09-11lib/test: refactoring, rename file test_buffer to buffer_writerShulhan
Continuing previous refactoring, we rename the file test_buffer to buffer_writer to match with its type name.
2023-09-11lib/test: refactoring, rename TestWriter to BufferWriterShulhan
The name TestWriter is considered stutter if its called from external package, test.TestWriter. While at it, implement the Error and Errorf in BufferWriter and add comment to each exported methods.
2023-09-11test/mock: implement mock for testing io.ReadWriter or io.StringWriterShulhan
The type ReadWriter provide two buffer, BufRead and BufWrite. The BufRead is for io.Reader and BufWrite for io.Writer or io.StringWriter. The type provide one method helper, Reset, to reset all buffers.
2023-05-30lib/test: update documentation related to Assert and DataShulhan
The documentation is based on the article published at https://kilabit.local/journal/2023/go_test_data/ after reviewing and explain how to use both of them to public.
2023-05-30lib/test: add another example for Assert-ing stringShulhan
This example show expected, got, and diff between them.
2023-05-25lib/test: use different package for exampleShulhan
The idea is to prevent using non-exported function in example. Case in point, in this changes the testWriter is used in example and not exported which make our test not runnable on public website. To fix that we export TestWriter with this changes.
2023-03-12lib/test: update the Assert comment related to latest changesShulhan
While at it, update the CHANGELOG to add that previous changes.
2023-03-02lib/test: simplify the string diff output from AssertShulhan
In the output, instead of using %q we replace it with %s, because printing string with double quote cause escaping and hard to read This change may cause difference in white spaces not showed in the terminal. In the diff changes, only print the Old and New, without printing each chunk.
2022-09-21lib/test: fix Assert always fail if no changes after string diffShulhan
While at it, update the Assert documentation by describing output lines after "----" and "++++".
2022-09-15lib/test: use text/diff to compare strings on AssertShulhan
If both exp and got types are string and its longer than 50 chars, it will use the text/diff.Text to show the difference between them. The diff output is as follow, !!! string not matched: --++ <LINE_NUM> - "<LINE_EXP>" <LINE_NUM> + "<LINE_GOT>" ^<COL_NUM> - "<DELETED_STRING>" ^<COL_NUM> + "<INSERTED_STRING>" The "<LINE_NUM> - " print the line number in exp followed by line itself. The "<LINE_NUM> + " print the line number in got followed by line itself. The "^<COL_NUM> - " show the character number in exp line followed by deleted string (or string that not exist in got). The "^<COL_NUM> + " show the character number in got line followed by inserted string (or string that not exist in exp).
2022-09-13lib/test: change the Assert parameter to WriterShulhan
Since we only need to call Log and Fatalf during Assert, no need to pass the whole instance of testing.T to Assert. By changing it to Writer, we also can test the Assert. This remove the AssertBench, because it have the same function parameters and body as Assert.
2022-07-25lib/test: truncate the last new line at the end of input and outputShulhan
Given the following input and output, >>> input <<< output EOF The input and output content always have new line at the end. This may cause unexpected input or output. If input or output content expecting new line at the end, add two empty lines at the end of it.
2022-07-23lib/test: sort the loaded test Data filesShulhan
This is to make the list of files being tested is consistent.
2022-07-23lib/test: load test data files with suffix "_test.txt"Shulhan
Previously, the LoadDataDir load all files from directory that have ".txt" extension. This may cause unknown non-test data files loaded and may cause an error. To distinguish it with other text files, inside the directory to be loaded load only the file that has suffix "_test.txt".
2022-07-22lib/test: implement Data, a type to load formatted file for helping testShulhan
Data contains predefined input and output values that is loaded from file to be used during test. The data provides zero or more flags, an optional description, zero or more input, and zero or more output. The data file name must end with ".txt". The data content use the following format, [FLAG_KEY ":" FLAG_VALUE LF] [LF DESCRIPTION] ">>>" [INPUT_NAME] LF INPUT_CONTENT LF "<<<" [OUTPUT_NAME] LF OUTPUT_CONTENT The data can contains zero or more flag. A flag is key and value separated by ":". The flag key must not contain spaces. The data may contain description. The line that start with "\n>>>" defined the beginning of input. An input can have a name, if its empty it will be set to "default". An input can be defined multiple times, with different names. The line that start with "\n<<<" defined the beginning of output. An output can have a name, if its empty it will be set to "default". An output also can be defined multiple times, with different names.
2022-07-19lib/test: realign struct in the testShulhan
2022-05-09all: reformat all codes using gofmt 1.19 (the Go tip)Shulhan
2022-04-06all: replace any usage of ioutil package with os or ioShulhan
Since Go 1.16, the ioutil package has been deprecated. This changes replace any usage that use functions from ioutil package with their replacement from package os or package io.
2021-03-14all: refactoring the test.Assert and test.AssertBench signatureShulhan
Previously, the test.Assert and test.AssertBench functions has the boolean parameter to print the stack trace of test in case its not equal. Since this parameter is not mandatory and its usually always set to "true", we remove them from function signature to simplify the call to Assert and AssertBench.
2020-11-23test: refactoring Assert with better error messageShulhan
The new Assert function use the reflect.DoEqual that return an error which describe which field have unmatched value. This changes affect other test that use "false" as the last parameter.
2020-06-06all: use default linter optionsShulhan
2020-03-20reflect: add function IsEqual that works with Equaler interfaceShulhan
The IsEqual() function is like reflect.DeepEqual but its check if a struct have method "IsEqual", if its exist it will call the method to compare the value.
2020-03-20test: check if exp implement reflect.EqualerShulhan
If the exp parameter implement the extended "reflect.Equaler" interface then it will use the method IsEqual() with "got" as parameter.
2019-09-25all: fix linter warningsShulhan
2019-06-14all: fix nolint formatShulhan
The valid syntax to suppress linter warnings is "//nolint:<name>" with no space between comment and "nolint" and between ":". Also, we move the placement of nolint directive to the top of statements for multiple nolint in the same scope. While at it, fix and supress some linter warnings.
2019-03-26test: change the error format to print "got" before "expectation"Shulhan
Following CodeReviewComments on Go wiki about Useful Test Failures [1], the common idiom here is that the actual value is printed first followed by the expected value. [1] https://github.com/golang/go/wiki/CodeReviewComments#useful-test-failures
2019-03-01all: fix warnings from linterShulhan
Most of the warnings caused by update to linter which cause global variables declared with grouping "( ... )" and that has been suppressed, are become false-positive again.
2018-11-30all: minimize and suppress linter warnings for global variablesShulhan
2018-11-29all: fix warning from lintersShulhan
2018-09-13lib/test/mock: mock the standard input with temporary fileShulhan
2018-09-12lib/test/mock: new package for mocking standard output and standard errorShulhan
This package can be useful for testing output of command that print message to stdout or stderr.
2018-09-01[chore] Update documentationsShulhan