-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle negative count arg in string.replace
Also update the name of the argument to be "count" instead of "maxsplit", to match the Starlark spec. This fixes #9181 Closes #9228. PiperOrigin-RevId: 309400161
- Loading branch information
1 parent
70c6f0a
commit 00ea8fa
Showing
6 changed files
with
120 additions
and
19 deletions.
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
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
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
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
76 changes: 76 additions & 0 deletions
76
src/test/java/com/google/devtools/build/lib/syntax/StringModuleTest.java
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,76 @@ | ||
// Copyright 2020 The Bazel Authors. 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 com.google.devtools.build.lib.syntax; | ||
|
||
import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
/** Tests for SkylarkStringModule. */ | ||
// TODO(bazel-team): Migrate these test cases back to string_misc.sky, once either | ||
// 1) --incompatible_string_replace_count has been flipped (#11244) and deleted, or 2) the | ||
// standalone Starlark interpreter and tests gain the ability to run with semantics flags. | ||
@RunWith(Parameterized.class) | ||
public class StringModuleTest extends EvaluationTestCase { | ||
|
||
@Parameters(name = "{index}: flag={0}") | ||
public static Iterable<? extends Object> data() { | ||
return Arrays.asList( | ||
"--incompatible_string_replace_count=false", "--incompatible_string_replace_count=true"); | ||
} | ||
|
||
@Parameter public String flag; | ||
|
||
@Test | ||
public void testReplace() throws Exception { | ||
// Test that the behaviour is the same for the basic case both before | ||
// and after the incompatible change. | ||
new Scenario(flag) | ||
.testEval("'banana'.replace('a', 'o')", "'bonono'") | ||
.testEval("'banana'.replace('a', 'o', 2)", "'bonona'") | ||
.testEval("'banana'.replace('a', 'o', 0)", "'banana'") | ||
.testEval("'banana'.replace('a', 'e')", "'benene'") | ||
.testEval("'banana'.replace('a', '$()')", "'b$()n$()n$()'") | ||
.testEval("'banana'.replace('a', '$')", "'b$n$n$'") | ||
.testEval("'b$()n$()n$()'.replace('$()', '$($())')", "'b$($())n$($())n$($())'") | ||
.testEval("'banana'.replace('a', 'e', 2)", "'benena'") | ||
.testEval("'banana'.replace('a', 'e', 0)", "'banana'") | ||
.testEval("'banana'.replace('', '-')", "'-b-a-n-a-n-a-'") | ||
.testEval("'banana'.replace('', '-', 2)", "'-b-anana'") | ||
.testEval("'banana'.replace('', '-', 0)", "'banana'") | ||
.testEval("'banana'.replace('', '')", "'banana'") | ||
.testEval("'banana'.replace('a', '')", "'bnn'") | ||
.testEval("'banana'.replace('a', '', 2)", "'bnna'"); | ||
} | ||
|
||
@Test | ||
public void testReplaceIncompatibleFlag() throws Exception { | ||
// Test the scenario that changes with the incompatible flag | ||
new Scenario("--incompatible_string_replace_count=false") | ||
.testEval("'banana'.replace('a', 'o', -2)", "'banana'") | ||
.testEval("'banana'.replace('a', 'e', -1)", "'banana'") | ||
.testEval("'banana'.replace('a', 'e', -10)", "'banana'") | ||
.testEval("'banana'.replace('', '-', -2)", "'banana'"); | ||
|
||
new Scenario("--incompatible_string_replace_count=true") | ||
.testEval("'banana'.replace('a', 'o', -2)", "'bonono'") | ||
.testEval("'banana'.replace('a', 'e', -1)", "'benene'") | ||
.testEval("'banana'.replace('a', 'e', -10)", "'benene'") | ||
.testEval("'banana'.replace('', '-', -2)", "'-b-a-n-a-n-a-'"); | ||
} | ||
} |
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