-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
V2: WIP: Tests for Replacer #2675
Merged
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3217eb3
Tests for Replacer: Replacer.Set and Replacer.Delete
de4d7af
Merge branch 'v2-mh' into v2-test-replacer
86d3749
update replacer test to new implementation
e74306d
fix replacer: counted position wrong if placeholder was found
bff45b8
fix replacer: found placeholder again, if it was a non-existing one
708ff9e
test with spaces between the placeholders as this could have a differ…
3040c1f
Tests for Replacer.Map
da777d0
Tests for Replacer.Set: check also for something like {l{test1}
f649628
fix replacer: fix multiple occurrence of phOpen sign
624ee88
Tests for Replacer: rewrite Set and ReplaceAll tests to use implement…
961b1ab
Merge branch 'v2-fix-replacer' into v2-test-replacer
6b02474
Merge remote-tracking branch 'mholt/v2' into v2-test-replacer
bdfe314
Tests for Replacer: rewrite Delete test to use implementation not int…
70994e6
Tests for Replacer: rewrite Map tests to use implementation not inter…
ab2bcf2
Tests for Replacer: add test for NewReplacer
23c1226
Tests for Replacer: add test for default replacements
e8830d9
Tests for Replacer: fixed and refactored tests
26e90b7
Tests for Replacer: moved default replacement tests to New-test
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 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,219 @@ | ||
// Copyright 2015 Matthew Holt and The Caddy Authors | ||
// | ||
// 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 caddy | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Tests the Set method by setting some variables and replacing them afterwards. | ||
func TestReplacerSet(t *testing.T) { | ||
replacer := NewReplacer() | ||
testInput := "" | ||
expected := "" | ||
|
||
// first add the variables | ||
for _, tc := range []struct { | ||
variable string | ||
value string | ||
}{ | ||
{ | ||
variable: "test1", | ||
value: "val1", | ||
}, | ||
{ | ||
variable: "asdf", | ||
value: "123", | ||
}, | ||
{ | ||
variable: "äöü", | ||
value: "098765", | ||
}, | ||
{ | ||
variable: "23456789", | ||
value: "öö_äü", | ||
}, | ||
{ | ||
variable: "with space", | ||
value: "space value", | ||
}, | ||
{ | ||
variable: "1", | ||
value: "test-123", | ||
}, | ||
{ | ||
variable: "mySuper_IP", | ||
value: "1.2.3.4", | ||
}, | ||
{ | ||
variable: "testEmpty", | ||
value: "", | ||
}, | ||
} { | ||
replacer.Set(tc.variable, tc.value) | ||
testInput += string(phOpen) + tc.variable + string(phClose) | ||
if tc.value == "" { | ||
expected += "EMPTY" | ||
} else { | ||
expected += tc.value | ||
} | ||
} | ||
|
||
testInput += string(phOpen) + "MyNotSetVariable" + string(phClose) | ||
expected += string(phOpen) + "MyNotSetVariable" + string(phClose) | ||
|
||
// then check if they are really replaced | ||
actual := replacer.ReplaceAll(testInput, "EMPTY") | ||
|
||
if actual != expected { | ||
t.Errorf("Expectd '%s', got '%s' for input '%s'", expected, actual, testInput) | ||
} | ||
} | ||
|
||
// Tests the Set method by setting some variables and replacing them afterwards. | ||
// This test adds spaces between the placeholder | ||
func TestReplacerSetWithSpaces(t *testing.T) { | ||
replacer := NewReplacer() | ||
testInput := "" | ||
expected := "" | ||
|
||
// first add the variables | ||
for _, tc := range []struct { | ||
variable string | ||
value string | ||
}{ | ||
{ | ||
variable: "test1", | ||
value: "val1", | ||
}, | ||
{ | ||
variable: "asdf", | ||
value: "123", | ||
}, | ||
{ | ||
variable: "äöü", | ||
value: "098765", | ||
}, | ||
{ | ||
variable: "23456789", | ||
value: "öö_äü", | ||
}, | ||
} { | ||
replacer.Set(tc.variable, tc.value) | ||
testInput += " " + string(phOpen) + tc.variable + string(phClose) + " " | ||
expected += " " | ||
if tc.value == "" { | ||
expected += "EMPTY" | ||
} else { | ||
expected += tc.value | ||
} | ||
expected += " " | ||
} | ||
|
||
testInput += " " + string(phOpen) + "MyNotSetVariable" + string(phClose) + " " | ||
expected += " " + string(phOpen) + "MyNotSetVariable" + string(phClose) + " " | ||
|
||
// then check if they are really replaced | ||
actual := replacer.ReplaceAll(testInput, "EMPTY") | ||
|
||
if actual != expected { | ||
t.Errorf("Expectd '%s', got '%s' for input '%s'", expected, actual, testInput) | ||
} | ||
} | ||
|
||
// Tests the Delete method by setting some variables, deleting some of them and replacing them afterwards. | ||
// The deleted ones should not be replaced. | ||
func TestReplacerDelete(t *testing.T) { | ||
replacer := NewReplacer() | ||
testInput := "" | ||
expected := "" | ||
toDeleteAfter := "toDeleteAfter" | ||
|
||
// first add the variables | ||
for _, tc := range []struct { | ||
variable string | ||
value string | ||
delete bool | ||
}{ | ||
{ | ||
variable: "test1", | ||
value: "val1", | ||
}, | ||
{ | ||
variable: "asdf", | ||
value: "123", | ||
delete: true, | ||
}, | ||
{ | ||
variable: "äöü", | ||
value: "098765", | ||
}, | ||
{ | ||
variable: "23456789", | ||
value: "öö_äü", | ||
delete: true, | ||
}, | ||
{ | ||
variable: "with space", | ||
value: "space value", | ||
delete: true, | ||
}, | ||
{ | ||
variable: "1", | ||
value: "test-123", | ||
}, | ||
{ // this one will be deleted after all were added and not instantly after adding | ||
variable: toDeleteAfter, | ||
value: "test-123", | ||
}, | ||
{ | ||
variable: "mySuper_IP", | ||
value: "1.2.3.4", | ||
}, | ||
{ | ||
variable: "testEmpty", | ||
value: "", | ||
delete: true, | ||
}, | ||
{ | ||
variable: "test2Empty", | ||
value: "", | ||
delete: true, | ||
}, | ||
} { | ||
replacer.Set(tc.variable, tc.value) | ||
testInput += string(phOpen) + tc.variable + string(phClose) | ||
if tc.delete { | ||
expected += string(phOpen) + tc.variable + string(phClose) | ||
replacer.Delete(tc.variable) | ||
} else if tc.variable == toDeleteAfter { | ||
expected += string(phOpen) + tc.variable + string(phClose) | ||
} else if tc.value == "" { | ||
expected += "EMPTY" | ||
} else { | ||
expected += tc.value | ||
} | ||
} | ||
|
||
// Delete one key after all other ones to test if deleting it not directly after adding also workS | ||
replacer.Delete(toDeleteAfter) | ||
|
||
// then check if they are really replaced (except deleted ones) | ||
actual := replacer.ReplaceAll(testInput, "EMPTY") | ||
|
||
if actual != expected { | ||
t.Errorf("Expectd '%s', got '%s' for input '%s'", expected, actual, testInput) | ||
} | ||
} |
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.
Good thinking to test unicode