1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-23 22:14:53 +02:00
Files
fp-go/v2/readeroption/from_test.go
Dr. Carsten Leue 23333ce52c doc: improve doc
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-11-12 11:08:18 +01:00

113 lines
3.1 KiB
Go

// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readeroption
import (
"context"
"testing"
O "github.com/IBM/fp-go/v2/option"
"github.com/stretchr/testify/assert"
)
func TestFrom0(t *testing.T) {
// Function that returns a value from context
getConfig := func(ctx context.Context) (string, bool) {
if val := ctx.Value("config"); val != nil {
return val.(string), true
}
return "", false
}
roFunc := From0(getConfig)
ro := roFunc()
// Test with value in context
ctx1 := context.WithValue(context.Background(), "config", "test-config")
result1 := ro(ctx1)
assert.Equal(t, O.Of("test-config"), result1)
// Test without value in context
ctx2 := context.Background()
result2 := ro(ctx2)
assert.Equal(t, O.None[string](), result2)
}
func TestFrom1(t *testing.T) {
// Function that looks up a value by key
lookup := func(ctx context.Context, key string) (int, bool) {
if val := ctx.Value(key); val != nil {
return val.(int), true
}
return 0, false
}
roFunc := From1(lookup)
// Test with value in context
ctx1 := context.WithValue(context.Background(), "count", 42)
result1 := roFunc("count")(ctx1)
assert.Equal(t, O.Of(42), result1)
// Test without value in context
ctx2 := context.Background()
result2 := roFunc("count")(ctx2)
assert.Equal(t, O.None[int](), result2)
}
func TestFrom2(t *testing.T) {
// Function that combines two parameters with context
combine := func(ctx context.Context, a string, b int) (string, bool) {
if ctx.Value("enabled") == true {
return a + ":" + string(rune('0'+b)), true
}
return "", false
}
roFunc := From2(combine)
// Test with enabled context
ctx1 := context.WithValue(context.Background(), "enabled", true)
result1 := roFunc("test", 5)(ctx1)
assert.Equal(t, O.Of("test:5"), result1)
// Test with disabled context
ctx2 := context.Background()
result2 := roFunc("test", 5)(ctx2)
assert.Equal(t, O.None[string](), result2)
}
func TestFrom3(t *testing.T) {
// Function that combines three parameters with context
combine := func(ctx context.Context, a string, b int, c bool) (string, bool) {
if ctx.Value("enabled") == true && c {
return a + ":" + string(rune('0'+b)), true
}
return "", false
}
roFunc := From3(combine)
// Test with enabled context and true flag
ctx1 := context.WithValue(context.Background(), "enabled", true)
result1 := roFunc("test", 5, true)(ctx1)
assert.Equal(t, O.Of("test:5"), result1)
// Test with false flag
result2 := roFunc("test", 5, false)(ctx1)
assert.Equal(t, O.None[string](), result2)
}