mirror of
				https://github.com/MontFerret/ferret.git
				synced 2025-10-30 23:37:40 +02:00 
			
		
		
		
	Feature/#8 datetime (#152)
This commit is contained in:
		
							
								
								
									
										9
									
								
								pkg/stdlib/datetime/lib.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								pkg/stdlib/datetime/lib.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| package datetime | ||||
|  | ||||
| import "github.com/MontFerret/ferret/pkg/runtime/core" | ||||
|  | ||||
| func NewLib() map[string]core.Function { | ||||
| 	return map[string]core.Function{ | ||||
| 		"NOW": Now, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										20
									
								
								pkg/stdlib/datetime/now.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								pkg/stdlib/datetime/now.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package datetime | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| ) | ||||
|  | ||||
| // Now returns new DateTime object with Time equal to time.Now(). | ||||
| // @returns (DateTime) - New DateTime object. | ||||
| func Now(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 0, 0) | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	return values.NewCurrentDateTime(), nil | ||||
| } | ||||
							
								
								
									
										57
									
								
								pkg/stdlib/datetime/now_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								pkg/stdlib/datetime/now_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| package datetime_test | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/stdlib/datetime" | ||||
|  | ||||
| 	. "github.com/smartystreets/goconvey/convey" | ||||
| ) | ||||
|  | ||||
| type testCase struct { | ||||
| 	Name      string | ||||
| 	Expected  core.Value | ||||
| 	TimeArg   time.Time | ||||
| 	Args      []core.Value | ||||
| 	ShouldErr bool | ||||
| } | ||||
|  | ||||
| func TestNow(t *testing.T) { | ||||
| 	tcs := []*testCase{ | ||||
| 		&testCase{ | ||||
| 			Name:     "When too many arguments", | ||||
| 			Expected: values.None, | ||||
| 			Args: []core.Value{ | ||||
| 				values.NewCurrentDateTime(), | ||||
| 			}, | ||||
| 			ShouldErr: true, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for _, tc := range tcs { | ||||
| 		tc.Do(t) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (tc *testCase) Do(t *testing.T) { | ||||
| 	Convey(tc.Name, t, func() { | ||||
| 		var expected core.Value | ||||
|  | ||||
| 		expected = values.NewDateTime(tc.TimeArg) | ||||
|  | ||||
| 		dt, err := datetime.Now(context.Background(), tc.Args...) | ||||
|  | ||||
| 		if tc.ShouldErr { | ||||
| 			So(err, ShouldBeError) | ||||
| 			expected = values.None | ||||
| 		} else { | ||||
| 			So(err, ShouldBeNil) | ||||
| 		} | ||||
|  | ||||
| 		So(dt, ShouldEqual, expected) | ||||
| 	}) | ||||
| } | ||||
| @@ -5,7 +5,6 @@ import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/stdlib/strings" | ||||
| 	. "github.com/smartystreets/goconvey/convey" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user