-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix DateEntry Validator overwrite #5866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arieroos
wants to merge
2
commits into
fyne-io:develop
Choose a base branch
from
arieroos:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package widget | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func Test_DateEntry(t *testing.T) { | ||
| dateEntry := NewDateEntry() | ||
|
|
||
| testDate := time.Date(2025, 07, 18, 0, 0, 0, 0, time.Local) | ||
| testDateText := testDate.Format(dateEntry.DateFormat()) | ||
|
|
||
| inputTests := []struct { | ||
| input string | ||
| wantErr bool | ||
| wantDate *time.Time | ||
| }{ | ||
| {input: testDateText, wantErr: false, wantDate: &testDate}, | ||
| {input: "", wantErr: false, wantDate: nil}, | ||
| {input: testDateText, wantErr: false, wantDate: &testDate}, | ||
| {input: "not a valid date", wantErr: true, wantDate: nil}, | ||
| } | ||
|
|
||
| for _, tt := range inputTests { | ||
| dateEntry.SetText("") | ||
| for _, r := range tt.input { | ||
| dateEntry.TypedRune(r) | ||
| } | ||
|
|
||
| assert.Equal(t, tt.input, dateEntry.Text) | ||
| if tt.wantErr { | ||
| assert.Error(t, dateEntry.validationError) | ||
| } else { | ||
| assert.NoError(t, dateEntry.validationError) | ||
| } | ||
|
|
||
| if tt.wantDate == nil { | ||
| assert.Nil(t, dateEntry.Date) | ||
| } else { | ||
| assert.NotNil(t, dateEntry.Date) | ||
| assert.Equal(t, tt.wantDate.Year(), dateEntry.Date.Year()) | ||
| assert.Equal(t, tt.wantDate.Month(), dateEntry.Date.Month()) | ||
| assert.Equal(t, tt.wantDate.Day(), dateEntry.Date.Day()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func Test_DateEntry_CustomValidator(t *testing.T) { | ||
| minDate := time.Date(1989, 9, 16, 0, 0, 0, 0, time.Local) | ||
|
|
||
| noDateErr := fmt.Errorf("Please choose a date") | ||
| invDateErr := fmt.Errorf("Invalid Date") | ||
| toSoonErr := fmt.Errorf("Please eneter a date after 16 September 1989") | ||
|
|
||
| dateEntry := NewDateEntry() | ||
| dateEntry.Validator = func(s string) error { | ||
| if s == "" { | ||
| return noDateErr | ||
| } | ||
|
|
||
| if t, err := time.Parse(dateEntry.DateFormat(), s); err != nil { | ||
| return invDateErr | ||
| } else { | ||
| if t.Before(minDate) { | ||
| return toSoonErr | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| testDate := time.Date(2025, 07, 18, 0, 0, 0, 0, time.Local) | ||
| testDateText := testDate.Format(dateEntry.DateFormat()) | ||
|
|
||
| toSoonDate := minDate.Add(time.Duration(time.Hour * -24)) | ||
| toSoonDateText := toSoonDate.Format(dateEntry.DateFormat()) | ||
|
|
||
| inputTests := []struct { | ||
| input string | ||
| wantErr error | ||
| wantDate *time.Time | ||
| }{ | ||
| {input: testDateText, wantErr: nil, wantDate: &testDate}, | ||
| {input: "", wantErr: noDateErr, wantDate: nil}, | ||
| {input: testDateText, wantErr: nil, wantDate: &testDate}, | ||
| {input: "not a valid date", wantErr: invDateErr, wantDate: nil}, | ||
| {input: testDateText, wantErr: nil, wantDate: &testDate}, | ||
| {input: toSoonDateText, wantErr: toSoonErr, wantDate: &toSoonDate}, | ||
| } | ||
|
|
||
| for _, tt := range inputTests { | ||
| dateEntry.SetText("") | ||
| for _, r := range tt.input { | ||
| dateEntry.TypedRune(r) | ||
| } | ||
|
|
||
| assert.Equal(t, tt.input, dateEntry.Text) | ||
| if tt.wantErr != nil { | ||
| assert.EqualError(t, dateEntry.validationError, tt.wantErr.Error()) | ||
| } else { | ||
| assert.NoError(t, dateEntry.validationError) | ||
| } | ||
|
|
||
| if tt.wantDate == nil { | ||
| assert.Nil(t, dateEntry.Date) | ||
| } else { | ||
| assert.NotNil(t, dateEntry.Date) | ||
| assert.Equal(t, tt.wantDate.Year(), dateEntry.Date.Year()) | ||
| assert.Equal(t, tt.wantDate.Month(), dateEntry.Date.Month()) | ||
| assert.Equal(t, tt.wantDate.Day(), dateEntry.Date.Day()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func Test_DateEntry_SetDate(t *testing.T) { | ||
| dateEntry := NewDateEntry() | ||
|
|
||
| assert.Nil(t, dateEntry.Date) | ||
| assert.Equal(t, "", dateEntry.Text) | ||
| assert.NoError(t, dateEntry.Validate()) | ||
|
|
||
| testDate := time.Date(2025, 07, 18, 0, 0, 0, 0, time.Local) | ||
| testDateText := testDate.Format(dateEntry.DateFormat()) | ||
| dateEntry.SetDate(&testDate) | ||
|
|
||
| assert.NotNil(t, dateEntry.Date) | ||
| assert.Equal(t, testDate.Year(), dateEntry.Date.Year()) | ||
| assert.Equal(t, testDate.Month(), dateEntry.Date.Month()) | ||
| assert.Equal(t, testDate.Day(), dateEntry.Date.Day()) | ||
| assert.Equal(t, testDateText, dateEntry.Text) | ||
| assert.NoError(t, dateEntry.Validate()) | ||
|
|
||
| dateEntry.SetDate(nil) | ||
|
|
||
| assert.Nil(t, dateEntry.Date) | ||
| assert.Equal(t, "", dateEntry.Text) | ||
| assert.NoError(t, dateEntry.Validate()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes more sense to be a new public API on the
fyne.Localetype, rather than on Entry. And FYI the Since line would be// Since: 2.7in this case, since 2.7 will be the next feature release of FyneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it makes more sense. However, the logic that I call for that method is in the widget package (under widget/locale.go). I can move the logic to the root fyne package, which would also improve fyne's current locale functionality. I just didn't want to take the liberty to make architectural decisions like that.