diff --git a/README.md b/README.md index e942983..1eb2b8c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ # tern -**`tern`** is a lightweight and versatile Go package designed to simplify conditional logic using concise ternary expressions. With `tern`, you can write clear and expressive code that is easy to read and maintain. +**`tern`** is a lightweight and versatile Go package designed to streamline conditional logic with concise ternary expressions, helping you write clean, expressive, and maintainable code with ease. ## README @@ -15,10 +15,10 @@ ## Features -- **Generic Support**: Fully utilizes Go’s generics, making it type-safe and reusable across a wide range of data types. -- **Flexible Logic**: Supports both direct boolean conditions and conditional functions. -- **Deferred Execution**: Allows lazy evaluation using functions to compute values only when needed, improving performance in certain cases. -- **Zero Value Handling**: Provides utilities to return zero values when no fallback is provided. +- **Generic Support**: Fully leverages Go’s generics for type safety and flexibility across various data types. +- **Flexible Logic**: Provides robust support for both boolean conditions and conditional functions. +- **Lazy Evaluation**: Optimizes performance by computing values only when necessary through deferred execution. +- **Zero Value Handling**: Offers utilities to return default zero values for any type when no fallback is supplied. ## Installation @@ -28,7 +28,7 @@ go get github.com/yyle88/tern ## Usage -The `tern` package offers various helper functions for different conditional scenarios: +The `tern` package provides multiple helper functions tailored to handle various conditional scenarios. ### Basic Usage @@ -41,25 +41,25 @@ import ( ) func main() { - // Simple conditional expression - result := tern.BVV(true, "A", "B") - fmt.Println(result) // Output: A + // Basic conditional selection + result := tern.BVV(true, "Option A", "Option B") + fmt.Println(result) // Output: Option A - // Deferred execution for the second value - result = tern.BVF(false, "A", func() string { return "Computed B" }) - fmt.Println(result) // Output: Computed B + // Deferred execution for fallback value + result = tern.BVF(false, "Default", func() string { return "Computed Fallback" }) + fmt.Println(result) // Output: Computed Fallback - // Using zero values as fallback - result = tern.BV(false, "A") + // Handling zero values as fallback + result = tern.BV(false, "Fallback") fmt.Println(result) // Output: (empty string) } ``` -### Available Functions +### Function Overview -The `tern` package provides the following functions based on condition types and value evaluation methods: +Here is an overview of the functions provided by the `tern` package: -| **Function** | **Condition Type** | **First Value** | **Second Value** | +| **Function** | **Condition Type** | **Primary Value** | **Fallback Value** | |--------------|--------------------|--------------------------|--------------------------| | `BVV` | `bool` | Direct value | Direct value | | `BVF` | `bool` | Direct value | Function returning value | @@ -72,21 +72,21 @@ The `tern` package provides the following functions based on condition types and ### Lazy Evaluation Example -Using deferred execution ensures unnecessary computations are avoided: +Deferred execution ensures unnecessary computations are avoided, making your code more efficient: ```go -func computeHeavyValue() string { - fmt.Println("Heavy computation...") +func expensiveComputation() string { + fmt.Println("Computing...") return "Heavy Result" } -result := tern.BVF(false, "Default", computeHeavyValue) -// Output: Default (computeHeavyValue() is not executed) +result := tern.BVF(false, "Default", expensiveComputation) +// Output: Default (expensiveComputation is not executed) ``` -### Custom Zero Values +### Working with Zero Values -The package uses Go’s `Zero[T]()`, which automatically provides the zero value for any type: +The package provides `Zero[T]()`, a utility that returns the zero value for any generic type: ```go package main @@ -102,200 +102,97 @@ func main() { } ``` -以下是对 `BV`、`BF`、`FV` 和 `FF` 函数的详细表格说明: - ---- - -### Utility Functions for Zero-Value Fallbacks - -| **Function** | **Condition Type** | **Primary Value** | **Fallback Value** | **Description** | -|--------------|--------------------|--------------------------|------------------------|--------------------------------------------------------------------------------------------------------------------------------------| -| `BV` | `bool` | Direct value | Zero value of type `T` | Returns the first value if the condition is true, otherwise returns the zero value of type `T`. | -| `BF` | `bool` | Function returning value | Zero value of type `T` | Evaluates the function only if the condition is true, otherwise returns the zero value of type `T`. | -| `FV` | `func() bool` | Direct value | Zero value of type `T` | Evaluates the condition function and returns the first value if true, otherwise returns the zero value of type `T`. | -| `FF` | `func() bool` | Function returning value | Zero value of type `T` | Evaluates the condition function and returns the result of the first function if true, otherwise returns the zero value of type `T`. | - --- -### Examples - -#### Using `BV` - -```go -result := tern.BV(false, "value") -// Output: (empty string, as the condition is false) -``` - -#### Using `BF` - -```go -result := tern.BF(false, func() string { return "value" }) -// Output: (empty string, as the condition is false) -``` - -#### Using `FV` - -```go -condition := func() bool { return true } -result := tern.FV(condition, "value") -// Output: "value" (as the condition function returns true) -``` +### Handling Zero-Value Fallbacks -#### Using `FF` +The package includes functions that automatically handle zero values when the condition is not met: -```go -condition := func() bool { return false } -result := tern.FF(condition, func() string { return "value" }) -// Output: (empty string, as the condition function returns false) -``` +| **Function** | **Condition Type** | **Primary Value** | **Fallback Value** | +|--------------|--------------------|--------------------------|--------------------------| +| `BV` | `bool` | Direct value | Zero value of type `T` | +| `BF` | `bool` | Function returning value | Zero value of type `T` | +| `FV` | `func() bool` | Direct value | Zero value of type `T` | +| `FF` | `func() bool` | Function returning value | Zero value of type `T` | --- -### Additional Utility Functions in `terngo` Package +### Additional Utilities in the `zerotern` Package -The `terngo` subpackage extends the functionality of `tern` with additional utility functions, specifically designed for comparing values to their zero value. These functions allow for more expressive handling of default values when one of the inputs might be zero. +The `zerotern` subpackage extends `tern` with specialized utilities for comparing values to their zero value, adding more control for fallback scenarios. -| **Function** | **Comparison Type** | **Primary Value** | **Fallback Value** | **Description** | -|--------------|---------------------|-------------------|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------| -| `VV` | Direct comparison | Direct value | Direct value | Returns the first value if it is not the zero value of type `T`, otherwise returns the second value. | -| `VF` | Direct comparison | Direct value | Function returning value | Returns the first value if it is not the zero value of type `T`, otherwise evaluates and returns the result of the fallback function. | - ---- +| **Function** | **Comparison Type** | **Primary Value** | **Fallback Value** | +|--------------|---------------------|--------------------------|--------------------------| +| `VV` | Direct comparison | Direct value | Direct value | +| `VF` | Direct comparison | Direct value | Function returning value | -### Examples - -#### Using `VV` +#### Example: Using `VV` and `VF` ```go package main import ( "fmt" - "github.com/yyle88/tern/terngo" + "github.com/yyle88/tern/zerotern" ) func main() { - result := terngo.VV("non-zero", "fallback") - fmt.Println(result) // Output: "non-zero" + // Direct comparison + result := zerotern.VV("non-zero", "fallback") + fmt.Println(result) // Output: non-zero - result = terngo.VV("", "fallback") - fmt.Println(result) // Output: "fallback" -} -``` - -#### Using `VF` - -```go -package main - -import ( - "fmt" - "github.com/yyle88/tern/terngo" -) - -func main() { - fallbackFunc := func() string { return "fallback from func" } - - result := terngo.VF("non-zero", fallbackFunc) - fmt.Println(result) // Output: "non-zero" - - result = terngo.VF("", fallbackFunc) - fmt.Println(result) // Output: "fallback from func" + // Fallback with function + result = zerotern.VF("", func() string { return "fallback func" }) + fmt.Println(result) // Output: fallback func } ``` --- -### Key Benefits of `VV` and `VF` +### Pointer-Based Utility Functions in `zerotern` -- **Zero-Value Handling**: These functions leverage `tern.Zero[T]()` to check if the primary value is a zero value. -- **Fallback Options**: `VF` provides flexibility by allowing dynamic evaluation of the fallback value through a function. - ---- +| **Function** | **Pointer Handling** | **Fallback Value** | +|--------------|-------------------------|--------------------------| +| `PV` | Pointer to direct value | Direct value | +| `PF` | Pointer to direct value | Function returning value | -By including these functions in the `terngo` package, developers gain even more expressive tools to simplify conditional logic in Go. - -### Additional Utility Functions for Pointer Modification in `terngo` - -The `terngo` subpackage introduces `PV` and `PF`, two specialized functions for safely modifying a pointer's value when it holds the zero value of its type. These functions ensure safer and more expressive manipulation of pointers while providing fallback options. - -| **Function** | **Pointer Check** | **Fallback Value** | **Description** | -|--------------|-------------------------|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------| -| `PV` | Pointer to direct value | Direct value | Sets the pointed-to value to the fallback value if it is the zero value of type `T`. Panics if the pointer is `nil`. | -| `PF` | Pointer to direct value | Function returning value | Sets the pointed-to value to the result of the fallback function if it is the zero value of type `T`. Panics if the pointer is `nil`. | - ---- - -### Examples - -#### Using `PV` +#### Example: Using `PV` and `PF` ```go package main import ( "fmt" - "github.com/yyle88/tern/terngo" + "github.com/yyle88/tern/zerotern" ) func main() { var value int - terngo.PV(&value, 42) + zerotern.PV(&value, 42) fmt.Println(value) // Output: 42 value = 7 - terngo.PV(&value, 99) - fmt.Println(value) // Output: 7 -} -``` - -#### Using `PF` - -```go -package main - -import ( - "fmt" - "github.com/yyle88/tern/terngo" -) - -func main() { - var value int - fallbackFunc := func() int { return 42 } - - terngo.PF(&value, fallbackFunc) - fmt.Println(value) // Output: 42 - - value = 7 - terngo.PF(&value, fallbackFunc) + zerotern.PF(&value, func() int { return 99 }) fmt.Println(value) // Output: 7 } ``` --- -### Key Features of `PV` and `PF` - -- **Pointer Safety**: These functions panic if the provided pointer is `nil`, ensuring reliable execution. -- **Zero-Value Check**: Use `tern.Zero[T]()` to determine whether the current value is the zero value of its type. -- **Dynamic Fallback**: `PF` allows for fallback values to be generated dynamically via a function. - -By leveraging `PV` and `PF`, developers can seamlessly handle zero-value assignments for pointer-based logic, maintaining both clarity and robustness in their Go code. - -## Why Use `tern`? +### Why Choose `tern`? -1. **Readability**: Simplifies conditional logic, making the code easier to follow. -2. **Flexibility**: Works seamlessly with both direct values and deferred computation. -3. **Performance**: Avoids unnecessary computation by using lazy evaluation. -4. **Generics**: Leverages Go’s generics for maximum flexibility and type safety. +1. **Improved Readability**: Simplifies conditional logic with concise and clear expressions. +2. **Performance Optimization**: Lazy evaluation avoids unnecessary computations. +3. **Type Safety**: Leverages Go’s generics for maximum flexibility and reliability. +4. **Versatility**: Supports a wide range of scenarios, including pointer handling and zero-value fallbacks. ## Contributing -Contributions are welcome! If you’d like to improve the package or fix a bug, feel free to open an issue or submit a pull request on [GitHub](https://github.com/yyle88/tern). +Contributions are welcome! Feel free to report issues, suggest improvements, or submit pull requests on [GitHub](https://github.com/yyle88/tern). ## License -This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details. +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. --- diff --git a/README.zh.md b/README.zh.md index dcc3df7..9f17712 100644 --- a/README.zh.md +++ b/README.zh.md @@ -1,17 +1,17 @@ -# tern +## tern -**`tern`** 是一个轻量且灵活的 Go 包,用于通过简洁的三元表达式简化条件逻辑。使用 `tern`,您可以编写清晰且易于维护的代码。 +**`tern`** 是一个轻量级且多功能的 Go 包,通过简洁的三元表达式简化条件逻辑,让您可以轻松编写清晰、简洁、可维护的代码。 -## README +## 说明文档 -[ENGLISH-DOC](README.md) +[English README](README.md) -## 功能特性 +## 功能特点 -- **泛型支持**:充分利用 Go 的泛型功能,实现类型安全,可复用性强。 -- **灵活的逻辑**:支持直接的布尔条件和基于函数的条件逻辑。 -- **延迟执行**:支持使用函数进行懒加载,仅在需要时计算值,从而提升性能。 -- **零值处理**:提供工具函数,当无后备值时返回类型的零值。 +- **泛型支持**:充分利用 Go 的泛型,类型安全且可重用,适用于各种数据类型。 +- **灵活逻辑**:支持直接的布尔条件和基于函数的条件判断。 +- **延迟执行**:支持使用函数延迟计算值,仅在需要时计算,有助于提高性能。 +- **零值处理**:提供实用函数,在没有备用值时返回类型的零值。 ## 安装 @@ -19,9 +19,9 @@ go get github.com/yyle88/tern ``` -## 使用说明 +## 用法 -`tern` 包提供了针对不同条件场景的多种辅助函数: +`tern` 包为不同条件场景提供了多种辅助函数: ### 基本用法 @@ -38,50 +38,48 @@ func main() { result := tern.BVV(true, "A", "B") fmt.Println(result) // 输出: A - // 为第二个值使用延迟执行 - result = tern.BVF(false, "A", func() string { return "计算出的 B" }) - fmt.Println(result) // 输出: 计算出的 B + // 使用延迟计算的第二个值 + result = tern.BVF(false, "A", func() string { return "计算后的B" }) + fmt.Println(result) // 输出: 计算后的B - // 使用零值作为后备值 + // 使用零值作为备用值 result = tern.BV(false, "A") - fmt.Println(result) // 输出: (空字符串) + fmt.Println(result) // 输出: (空字符串) } ``` -### 提供的函数 +### 可用函数 -`tern` 包根据条件类型和值的计算方式提供以下函数: +以下表格列出了 `tern` 提供的函数,按条件类型和值的计算方法分类: -| **函数名称** | **条件类型** | **第一个值** | **第二个值** | -|----------|---------------|----------|----------| -| `BVV` | `bool` | 直接值 | 直接值 | -| `BVF` | `bool` | 直接值 | 返回值的函数 | -| `BFV` | `bool` | 返回值的函数 | 直接值 | -| `BFF` | `bool` | 返回值的函数 | 返回值的函数 | -| `FVV` | `func() bool` | 直接值 | 直接值 | -| `FVF` | `func() bool` | 直接值 | 返回值的函数 | -| `FFV` | `func() bool` | 返回值的函数 | 直接值 | -| `FFF` | `func() bool` | 返回值的函数 | 返回值的函数 | - -此外,诸如 `BV`、`BF`、`FV` 和 `FF` 等工具函数用于简化返回零值的场景。 +| **函数** | **条件类型** | **第一个值** | **第二个值** | +|--------|---------------|----------|----------| +| `BVV` | `bool` | 直接值 | 直接值 | +| `BVF` | `bool` | 直接值 | 函数返回值 | +| `BFV` | `bool` | 函数返回值 | 直接值 | +| `BFF` | `bool` | 函数返回值 | 函数返回值 | +| `FVV` | `func() bool` | 直接值 | 直接值 | +| `FVF` | `func() bool` | 直接值 | 函数返回值 | +| `FFV` | `func() bool` | 函数返回值 | 直接值 | +| `FFF` | `func() bool` | 函数返回值 | 函数返回值 | ### 延迟计算示例 -使用延迟执行可以避免不必要的计算: +使用延迟计算可以避免不必要的开销: ```go func computeHeavyValue() string { - fmt.Println("执行重计算...") + fmt.Println("执行复杂计算...") return "计算结果" } result := tern.BVF(false, "默认值", computeHeavyValue) -// 输出: 默认值 (computeHeavyValue() 不会被执行) +// 输出: 默认值(computeHeavyValue() 未被执行) ``` ### 自定义零值 -该包使用 Go 的 `Zero[T]()`,可自动为任何类型提供零值: +`tern` 使用 Go 的 `Zero[T]()` 方法,根据类型自动提供零值: ```go package main @@ -93,24 +91,191 @@ import ( func main() { fmt.Println(tern.Zero[int]()) // 输出: 0 - fmt.Println(tern.Zero[string]()) // 输出: (空字符串) + fmt.Println(tern.Zero[string]()) // 输出: (空字符串) } ``` +--- + +### 基于零值的备用处理函数 + +| **函数** | **条件类型** | **主值** | **备用值** | **描述** | +|--------|---------------|--------|---------|--------------------------------------| +| `BV` | `bool` | 直接值 | 类型的零值 | 如果条件为真,则返回第一个值;否则返回类型的零值。 | +| `BF` | `bool` | 函数返回值 | 类型的零值 | 如果条件为真,则计算函数并返回结果;否则返回类型的零值。 | +| `FV` | `func() bool` | 直接值 | 类型的零值 | 计算条件函数,如果为真,则返回第一个值;否则返回类型的零值。 | +| `FF` | `func() bool` | 函数返回值 | 类型的零值 | 计算条件函数,如果为真,则计算第一个函数并返回结果;否则返回类型的零值。 | + +--- + +### 示例 + +#### 使用 `BV` + +```go +result := tern.BV(false, "值") +// 输出: (空字符串,因为条件为假) +``` + +#### 使用 `BF` + +```go +result := tern.BF(false, func() string { return "值" }) +// 输出: (空字符串,因为条件为假) +``` + +#### 使用 `FV` + +```go +condition := func() bool { return true } +result := tern.FV(condition, "值") +// 输出: "值"(因为条件函数返回 true) +``` + +#### 使用 `FF` + +```go +condition := func() bool { return false } +result := tern.FF(condition, func() string { return "值" }) +// 输出: (空字符串,因为条件函数返回 false) +``` + +--- + +### `zerotern` 子包中的附加工具函数 + +`zerotern` 子包扩展了 `tern` 的功能,提供了更多与零值比较相关的实用函数。这些函数支持在输入可能为零值时更清晰地处理默认值。 + +| **函数** | **比较类型** | **主值** | **备用值** | **描述** | +|--------|----------|--------|---------|------------------------------------| +| `VV` | 直接比较 | 直接值 | 直接值 | 如果第一个值不是类型的零值,则返回第一个值;否则返回第二个值。 | +| `VF` | 直接比较 | 直接值 | 函数返回值 | 如果第一个值不是类型的零值,则返回第一个值;否则计算函数并返回结果。 | + +--- + +### 示例 + +#### 使用 `VV` + +```go +package main + +import ( + "fmt" + "github.com/yyle88/tern/zerotern" +) + +func main() { + result := zerotern.VV("非零值", "备用值") + fmt.Println(result) // 输出: "非零值" + + result = zerotern.VV("", "备用值") + fmt.Println(result) // 输出: "备用值" +} +``` + +#### 使用 `VF` + +```go +package main + +import ( + "fmt" + "github.com/yyle88/tern/zerotern" +) + +func main() { + fallbackFunc := func() string { return "函数计算的备用值" } + + result := zerotern.VF("非零值", fallbackFunc) + fmt.Println(result) // 输出: "非零值" + + result = zerotern.VF("", fallbackFunc) + fmt.Println(result) // 输出: "函数计算的备用值" +} +``` + +--- + +### 基于指针的工具函数 (`zerotern` 子包) + +`zerotern` 子包提供了一组基于指针的赋值工具函数,用于检查指针内容是否为零值,并根据需要更新其内容。这些函数可以简化对指针类型值的默认处理逻辑。 + +| **函数** | **参数类型** | **主值** | **备用值** | **描述** | +|--------|----------|--------|---------|-------------------------------| +| `PV` | 指针及备用值 | 指针指向的值 | 直接值 | 如果指针内容为零值,则将指针内容更新为备用值。 | +| `PF` | 指针及备用值 | 指针指向的值 | 函数返回值 | 如果指针内容为零值,则通过函数计算备用值,再更新指针内容。 | + +--- + +### 示例 + +#### 使用 `PV` + +```go +package main + +import ( + "fmt" + "github.com/yyle88/tern/zerotern" +) + +func main() { + value := "" + ptr := &value + + zerotern.PV(ptr, "备用值") + fmt.Println(*ptr) // 输出: "备用值" + + zerotern.PV(ptr, "新值") + fmt.Println(*ptr) // 输出: "备用值"(因为内容已经非零值) +} +``` + +#### 使用 `PF` + +```go +package main + +import ( + "fmt" + "github.com/yyle88/tern/zerotern" +) + +func main() { + value := "" + ptr := &value + + fallbackFunc := func() string { return "函数计算的备用值" } + + zerotern.PF(ptr, fallbackFunc) + fmt.Println(*ptr) // 输出: "函数计算的备用值" + + zerotern.PF(ptr, func() string { return "新计算值" }) + fmt.Println(*ptr) // 输出: "函数计算的备用值"(因为内容已经非零值) +} +``` + +--- + +通过 `PV` 和 `PF`,`zerotern` 子包可以有效地处理指针内容的零值逻辑,提供了一种简洁而高效的方式来实现默认值赋值。 + +--- + ## `tern` 有什么优势? -1. **可读性**:简化条件逻辑,使代码更易理解。 -2. **灵活性**:兼容直接值和延迟计算。 -3. **性能**:通过懒加载避免不必要的计算。 -4. **泛型支持**:利用 Go 泛型实现最大灵活性和类型安全。 +1. **可读性**:简化条件逻辑,代码更加易读。 +2. **灵活性**:同时支持直接值和延迟计算。 +3. **性能优化**:通过延迟计算避免不必要的计算。 +4. **泛型支持**:利用 Go 的泛型,实现灵活且类型安全的逻辑处理。 -## 贡献指南 +## 参与贡献 -欢迎贡献代码!如果您希望改进此包或修复 Bug,请随时在 [GitHub](https://github.com/yyle88/tern) 上提交 issue 或 pull request。 +欢迎贡献代码!如果您有改进建议或发现了问题,可以在 [GitHub](https://github.com/yyle88/tern) 提交 Issue 或 Pull Request。 ## 许可证 -项目采用 MIT 许可证。详情请参阅 [LICENSE](LICENSE) 文件。 +项目使用 MIT 许可证,详见 [LICENSE](LICENSE) 文件。 ## 其它 diff --git a/terngo/znew_test.go b/terngo/znew_test.go deleted file mode 100644 index 6d19e65..0000000 --- a/terngo/znew_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package terngo_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/yyle88/tern/terngo" -) - -func TestVV(t *testing.T) { - require.Equal(t, 1, terngo.VV(0, 1)) - require.Equal(t, 1, terngo.VV(1, 2)) - require.Equal(t, "a", terngo.VV("", "a")) - require.Equal(t, "a", terngo.VV("a", "b")) -} - -func TestVF(t *testing.T) { - require.Equal(t, 1, terngo.VF(0, func() int { - return 1 - })) - require.Equal(t, 1, terngo.VF(1, func() int { - return 2 - })) - require.Equal(t, "a", terngo.VF("", func() string { - return "a" - })) - require.Equal(t, "a", terngo.VF("a", func() string { - return "b" - })) -} diff --git a/terngo/znew.go b/zerotern/vorx.go similarity index 92% rename from terngo/znew.go rename to zerotern/vorx.go index 9376e80..baa8eaa 100644 --- a/terngo/znew.go +++ b/zerotern/vorx.go @@ -1,4 +1,4 @@ -package terngo +package zerotern import "github.com/yyle88/tern" diff --git a/zerotern/vorx_test.go b/zerotern/vorx_test.go new file mode 100644 index 0000000..3a4afa2 --- /dev/null +++ b/zerotern/vorx_test.go @@ -0,0 +1,30 @@ +package zerotern_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/tern/zerotern" +) + +func TestVV(t *testing.T) { + require.Equal(t, 1, zerotern.VV(0, 1)) + require.Equal(t, 1, zerotern.VV(1, 2)) + require.Equal(t, "a", zerotern.VV("", "a")) + require.Equal(t, "a", zerotern.VV("a", "b")) +} + +func TestVF(t *testing.T) { + require.Equal(t, 1, zerotern.VF(0, func() int { + return 1 + })) + require.Equal(t, 1, zerotern.VF(1, func() int { + return 2 + })) + require.Equal(t, "a", zerotern.VF("", func() string { + return "a" + })) + require.Equal(t, "a", zerotern.VF("a", func() string { + return "b" + })) +} diff --git a/terngo/zset.go b/zerotern/zset.go similarity index 92% rename from terngo/zset.go rename to zerotern/zset.go index 6402db9..ed85b31 100644 --- a/terngo/zset.go +++ b/zerotern/zset.go @@ -1,4 +1,4 @@ -package terngo +package zerotern import "github.com/yyle88/tern" diff --git a/terngo/zset_test.go b/zerotern/zset_test.go similarity index 63% rename from terngo/zset_test.go rename to zerotern/zset_test.go index 3291250..039ffd0 100644 --- a/terngo/zset_test.go +++ b/zerotern/zset_test.go @@ -1,51 +1,51 @@ -package terngo_test +package zerotern_test import ( "testing" "github.com/stretchr/testify/require" - "github.com/yyle88/tern/terngo" + "github.com/yyle88/tern/zerotern" ) func TestPV(t *testing.T) { a := 0 - terngo.PV(&a, 1) + zerotern.PV(&a, 1) require.Equal(t, 1, a) b := 1 - terngo.PV(&b, 2) + zerotern.PV(&b, 2) require.Equal(t, 1, b) c := "" - terngo.PV(&c, "a") + zerotern.PV(&c, "a") require.Equal(t, "a", c) s := "a" - terngo.PV(&s, "b") + zerotern.PV(&s, "b") require.Equal(t, "a", s) } func TestPF(t *testing.T) { a := 0 - terngo.PF(&a, func() int { + zerotern.PF(&a, func() int { return 1 }) require.Equal(t, 1, a) b := 1 - terngo.PF(&b, func() int { + zerotern.PF(&b, func() int { return 2 }) require.Equal(t, 1, b) c := "" - terngo.PF(&c, func() string { + zerotern.PF(&c, func() string { return "a" }) require.Equal(t, "a", c) s := "a" - terngo.PF(&s, func() string { + zerotern.PF(&s, func() string { return "b" }) require.Equal(t, "a", s)