123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- // Copyright 2016 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- //go:build go1.10
- package bidirule
- import (
- "golang.org/x/text/transform"
- "golang.org/x/text/unicode/bidi"
- )
- var testCases = [][]ruleTest{
- // Go-specific rules.
- // Invalid UTF-8 is invalid.
- 0: []ruleTest{{
- in: "",
- dir: bidi.LeftToRight,
- }, {
- in: "\x80",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 0,
- }, {
- in: "\xcc",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 0,
- }, {
- in: "abc\x80",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 3,
- }, {
- in: "abc\xcc",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 3,
- }, {
- in: "abc\xccdef",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 3,
- }, {
- in: "\xccdef",
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: 0,
- }, {
- in: strR + "\x80",
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: len(strR),
- }, {
- in: strR + "\xcc",
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: len(strR),
- }, {
- in: strAL + "\xcc" + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: len(strAL),
- }, {
- in: "\xcc" + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 0,
- }},
- // Rule 2.1: The first character must be a character with Bidi property L,
- // R, or AL. If it has the R or AL property, it is an RTL label; if it has
- // the L property, it is an LTR label.
- 1: []ruleTest{{
- in: strL,
- dir: bidi.LeftToRight,
- }, {
- in: strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strAN,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- }, {
- in: strEN,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strEN),
- }, {
- in: strES,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strES),
- }, {
- in: strET,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strET),
- }, {
- in: strCS,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strCS),
- }, {
- in: strNSM,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strNSM),
- }, {
- in: strBN,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strBN),
- }, {
- in: strB,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strB),
- }, {
- in: strS,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strS),
- }, {
- in: strWS,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strWS),
- }, {
- in: strON,
- dir: bidi.LeftToRight,
- err: ErrInvalid,
- n: len(strON),
- }, {
- in: strEN + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 3,
- }, {
- in: strES + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 2,
- }, {
- in: strET + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 1,
- }, {
- in: strCS + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 1,
- }, {
- in: strNSM + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 2,
- }, {
- in: strBN + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 3,
- }, {
- in: strB + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 3,
- }, {
- in: strS + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 1,
- }, {
- in: strWS + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 1,
- }, {
- in: strON + strR,
- dir: bidi.RightToLeft,
- err: ErrInvalid,
- n: 1,
- }},
- // Rule 2.2: In an RTL label, only characters with the Bidi properties R,
- // AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
- 2: []ruleTest{{
- in: strR + strR + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strAL + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strAN + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strEN + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strES + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strCS + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strET + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strON + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strBN + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strNSM + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strL + strR,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strB + strR,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strS + strAL,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strWS + strAL,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strAL + strR + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strAL + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strAN + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strEN + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strES + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strCS + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strET + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strON + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strBN + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strNSM + strAL,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strL + strR,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strB + strR,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strS + strAL,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strWS + strAL,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }},
- // Rule 2.3: In an RTL label, the end of the label must be a character with
- // Bidi property R, AL, EN, or AN, followed by zero or more characters with
- // Bidi property NSM.
- 3: []ruleTest{{
- in: strR + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strAL + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strEN + strNSM + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strAN,
- dir: bidi.RightToLeft,
- }, {
- in: strR + strES + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR + strES + strNSM),
- err: ErrInvalid,
- }, {
- in: strR + strCS + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR + strCS + strNSM + strNSM),
- err: ErrInvalid,
- }, {
- in: strR + strET,
- dir: bidi.RightToLeft,
- n: len(strR + strET),
- err: ErrInvalid,
- }, {
- in: strR + strON + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR + strON + strNSM),
- err: ErrInvalid,
- }, {
- in: strR + strBN + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR + strBN + strNSM + strNSM),
- err: ErrInvalid,
- }, {
- in: strR + strL + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strB + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strS,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strR + strWS,
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- }, {
- in: strAL + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strR,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strAL + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strEN + strNSM + strNSM,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strAN,
- dir: bidi.RightToLeft,
- }, {
- in: strAL + strES + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL + strES + strNSM),
- err: ErrInvalid,
- }, {
- in: strAL + strCS + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL + strCS + strNSM + strNSM),
- err: ErrInvalid,
- }, {
- in: strAL + strET,
- dir: bidi.RightToLeft,
- n: len(strAL + strET),
- err: ErrInvalid,
- }, {
- in: strAL + strON + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL + strON + strNSM),
- err: ErrInvalid,
- }, {
- in: strAL + strBN + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL + strBN + strNSM + strNSM),
- err: ErrInvalid,
- }, {
- in: strAL + strL + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strB + strNSM + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strS,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }, {
- in: strAL + strWS,
- dir: bidi.RightToLeft,
- n: len(strAL),
- err: ErrInvalid,
- }},
- // Rule 2.4: In an RTL label, if an EN is present, no AN may be present,
- // and vice versa.
- 4: []ruleTest{{
- in: strR + strEN + strAN,
- dir: bidi.RightToLeft,
- n: len(strR + strEN),
- err: ErrInvalid,
- }, {
- in: strR + strAN + strEN + strNSM,
- dir: bidi.RightToLeft,
- n: len(strR + strAN),
- err: ErrInvalid,
- }, {
- in: strAL + strEN + strAN,
- dir: bidi.RightToLeft,
- n: len(strAL + strEN),
- err: ErrInvalid,
- }, {
- in: strAL + strAN + strEN + strNSM,
- dir: bidi.RightToLeft,
- n: len(strAL + strAN),
- err: ErrInvalid,
- }},
- // Rule 2.5: In an LTR label, only characters with the Bidi properties L,
- // EN, ES, CS, ET, ON, BN, or NSM are allowed.
- 5: []ruleTest{{
- in: strL + strL + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strEN + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strES + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strCS + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strET + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strON + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strBN + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strNSM + strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strR + strL,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strAL + strL,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strAN + strL,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strB + strL,
- dir: bidi.LeftToRight,
- n: len(strL + strB + strL),
- err: ErrInvalid,
- }, {
- in: strL + strB + strL + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strB + strL),
- err: ErrInvalid,
- }, {
- in: strL + strS + strL,
- dir: bidi.LeftToRight,
- n: len(strL + strS + strL),
- err: ErrInvalid,
- }, {
- in: strL + strS + strL + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strS + strL),
- err: ErrInvalid,
- }, {
- in: strL + strWS + strL,
- dir: bidi.LeftToRight,
- n: len(strL + strWS + strL),
- err: ErrInvalid,
- }, {
- in: strL + strWS + strL + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strWS + strL),
- err: ErrInvalid,
- }},
- // Rule 2.6: In an LTR label, the end of the label must be a character with
- // Bidi property L or EN, followed by zero or more characters with Bidi
- // property NSM.
- 6: []ruleTest{{
- in: strL,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strNSM,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strNSM + strNSM,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strEN,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strEN + strNSM,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strEN + strNSM + strNSM,
- dir: bidi.LeftToRight,
- }, {
- in: strL + strES,
- dir: bidi.LeftToRight,
- n: len(strL + strES),
- err: ErrInvalid,
- }, {
- in: strL + strES + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strES),
- err: ErrInvalid,
- }, {
- in: strL + strCS,
- dir: bidi.LeftToRight,
- n: len(strL + strCS),
- err: ErrInvalid,
- }, {
- in: strL + strCS + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strCS),
- err: ErrInvalid,
- }, {
- in: strL + strET,
- dir: bidi.LeftToRight,
- n: len(strL + strET),
- err: ErrInvalid,
- }, {
- in: strL + strET + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strET),
- err: ErrInvalid,
- }, {
- in: strL + strON,
- dir: bidi.LeftToRight,
- n: len(strL + strON),
- err: ErrInvalid,
- }, {
- in: strL + strON + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strON),
- err: ErrInvalid,
- }, {
- in: strL + strBN,
- dir: bidi.LeftToRight,
- n: len(strL + strBN),
- err: ErrInvalid,
- }, {
- in: strL + strBN + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strBN),
- err: ErrInvalid,
- }, {
- in: strL + strR,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strAL,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strAN,
- dir: bidi.RightToLeft,
- n: len(strL),
- err: ErrInvalid,
- }, {
- in: strL + strB,
- dir: bidi.LeftToRight,
- n: len(strL + strB),
- err: ErrInvalid,
- }, {
- in: strL + strB + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strB),
- err: ErrInvalid,
- }, {
- in: strL + strS,
- dir: bidi.LeftToRight,
- n: len(strL + strS),
- err: ErrInvalid,
- }, {
- in: strL + strS + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strS),
- err: ErrInvalid,
- }, {
- in: strL + strWS,
- dir: bidi.LeftToRight,
- n: len(strL + strWS),
- err: ErrInvalid,
- }, {
- in: strL + strWS + strR,
- dir: bidi.RightToLeft,
- n: len(strL + strWS),
- err: ErrInvalid,
- }},
- // Incremental processing.
- 9: []ruleTest{{
- in: "e\u0301", // é
- dir: bidi.LeftToRight,
- pSrc: 2,
- nSrc: 1,
- err0: transform.ErrShortSrc,
- }, {
- in: "e\u1000f", // é
- dir: bidi.LeftToRight,
- pSrc: 3,
- nSrc: 1,
- err0: transform.ErrShortSrc,
- }, {
- // Remain invalid once invalid.
- in: strR + "ab",
- dir: bidi.RightToLeft,
- n: len(strR),
- err: ErrInvalid,
- pSrc: len(strR) + 1,
- nSrc: len(strR),
- err0: ErrInvalid,
- }, {
- // Short destination
- in: "abcdefghij",
- dir: bidi.LeftToRight,
- pSrc: 10,
- szDst: 5,
- nSrc: 5,
- err0: transform.ErrShortDst,
- }, {
- in: "\U000102f7",
- dir: bidi.LeftToRight,
- n: len("\U000102f7"),
- err: ErrInvalid,
- }, {
- // Short destination splitting input rune
- in: "e\u0301",
- dir: bidi.LeftToRight,
- pSrc: 3,
- szDst: 2,
- nSrc: 1,
- err0: transform.ErrShortDst,
- }, {
- // Unicode 10.0.0 IDNA test string.
- in: "FAX\u2a77\U0001d186",
- dir: bidi.LeftToRight,
- n: len("FAX\u2a77\U0001d186"),
- err: ErrInvalid,
- }, {
- in: "\x80\u0660",
- dir: bidi.RightToLeft,
- n: 0,
- err: ErrInvalid,
- }},
- }
|