Skip to content

Commit

Permalink
✔️🏁 processArgsRm added + tests
Browse files Browse the repository at this point in the history
 Issues related:
 	-> #21
 	-> #16
 	-> #9

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/init_test.go
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
  • Loading branch information
ChacaS0 committed Mar 27, 2018
1 parent 187acb9 commit fb436d1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
16 changes: 16 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func fbTestTempestcf(t *testing.T, tempestcfbup string) {
// SameSlices checks equality between two slices of string
// returns true if they are identiques
func SameSlices(a, b []string) bool {
if a == nil && nil == b {
return true
}

if len(a) == 0 && len(b) == 0 {
return true
}

if len(a) != len(b) {
return false
}
Expand All @@ -195,6 +203,14 @@ func SameSlices(a, b []string) bool {
// SameSlicesInt checks equality between two slices of int
// returns true if they are identiques
func SameSlicesInt(a, b []int) bool {
if a == nil && nil == b {
return true
}

if len(a) == 0 && len(b) == 0 {
return true
}

if len(a) != len(b) {
return false
}
Expand Down
55 changes: 41 additions & 14 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,61 @@ func processArgsRm(args []string) ([]int, []string) {
strified += valStr + " "
}

// regexes
// RegexManyInt is the regex to see if many int were passed
// i.e. ``0 1 4`` or ``0``
RegexManyInt := regexp.MustCompile(`([0-9]+\s|[0-9]+$|^[0-9]+$)`)
// // regexes
// // RegexManyInt is the regex to see if many int were passed
// // i.e. ``0 1 4`` or ``0``
// // RegexManyInt := regexp.MustCompile(`([0-9]+\s|[0-9]|^[0-9]+$)`)
// RegexIntToInt is the regex to see if we want a range of int
// i.e. ``0-2`` (would be 0 1 2)
RegexIntToInt := regexp.MustCompile(`([0-9]+-[0-9]+)`)

if allItoI := RegexIntToInt.FindAllString(strified, -1); len(allItoI) > 0 {
RegexIntToInt.ReplaceAllString(strified, ``)
for _, exp := range allItoI {
RegexIntToInt := regexp.MustCompile(`(\d+-\d+)`)
// RegexManyStr is the regex to see if many strings (targets) were passed
// i.e. ``/tmp`` /path1/subpath1``
RegexManyStr := regexp.MustCompile(`^(\/|\\)(\d|\D)+`)
// RegexJustInt is the regex to see if only an int has been passed as an arg
RegexJustInt := regexp.MustCompile(`^(\d+)$`)

// TODO - Check if the value hasn't been provided twice (specially with range stuffs)
for _, arg := range args { // for each arg in args
arg = strings.Trim(arg, " ")
// IntToInt
if allItoI := RegexIntToInt.FindString(arg); len(allItoI) > 0 {
// RegexIntToInt.ReplaceAllString(arg, ``)
// explode with ``-`` and get the left and right value
values := strings.Split(exp, "-")
values := strings.Split(arg, "-")
begin, errBegin := strconv.Atoi(values[0])
end, errEnd := strconv.Atoi(values[1])
if errBegin != nil || errEnd != nil {
fmt.Println(redB(":: [ERROR]"), color.HiRedString("Sorry, could not understand those arguments", exp), "\n\t[0]:", values[0], "\n\t->", errBegin, "\n\t[1]:", values[1], "\n\t->", errEnd)
fmt.Println(redB(":: [ERROR]"), color.HiRedString("Sorry, could not understand those arguments", arg), "\n\t[0]:", values[0], "\n\t->", errBegin, "\n\t[1]:", values[1], "\n\t->", errEnd)
return nil, nil
}
if begin > end {
temp := end
end = begin
begin = temp
}
for i := begin; i <= end; i++ {
slRmInt = append(slRmInt, i)
}
}
}
// Many Strings
if allMStr := RegexManyStr.FindString(arg); len(allMStr) > 0 {
slRmStr = append(slRmStr, arg)
}
// Just an Int
if allInt := RegexJustInt.FindString(arg); len(allInt) > 0 {
val, errInt := strconv.Atoi(arg)
if errInt != nil {
fmt.Println(redB(":: [ERROR]"), color.HiRedString("Sorry, could not understand thise argument", arg), "\n\t->", errInt)
return nil, nil
}
slRmInt = append(slRmInt, val)
}

if RegexManyInt.FindString(strified) == strified {
// then that's it
}

// // if RegexManyInt.FindString(strified) == strified {
// // // then that's it
// // }
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> NOTE: WTFFFFFF!!?????
// for _, arg := range args {
// switch {
Expand Down
42 changes: 39 additions & 3 deletions cmd/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,25 @@ func TestWriteTempestcf(t *testing.T) {
// It verifies that it returns the right slices
func TestProcessArgsRm(t *testing.T) {
// parameter variables
//empty
s3 := []string{""}
// index ranges
s1 := []string{"0-1"}
s2 := []string{"0-2", "5-7"}
s3 := []string{""}
s12 := []string{"1-2", "5-3", "7-8"}
// Just targets
s4 := []string{"/temp", "/tmp/user/aur"}
s5 := []string{"/tmp"}
// Just Ints
s6 := []string{"0", "2", "5"}
s7 := []string{"1"}
// mixed
s8 := []string{"0", "2-5", "7", "/tmp"}
s9 := []string{"/tmp", "7", "2-5"}
s10 := []string{"2-5", "/tmp", "7"}
s11 := []string{"1-2", "3-4", "/temp", "5", "/tmp/user/aur", "6"}
// Overlaping arguments
// TODO

emptySliceStr := make([]string, 0)
emptySliceInt := make([]int, 0)
Expand All @@ -173,15 +189,35 @@ func TestProcessArgsRm(t *testing.T) {
wantSlStr []string
err error
}{
// Empty
{s3, emptySliceInt, emptySliceStr, errors.New("[FAIL]:: Failed to prcess empty args")},
// IntToInt
{s1, []int{0, 1}, emptySliceStr, errors.New("[FAIL]:: Failed to return the slice of ints")},
{s2, []int{0, 1, 2, 5, 6, 7}, emptySliceStr, errors.New("[FAIL]:: Failed to process 2 ranges of ints")},
{s3, emptySliceInt, emptySliceStr, errors.New("[FAIL]:: Failed to prcess empty args")},
{s12, []int{1, 2, 3, 4, 5, 7, 8}, emptySliceStr, errors.New("[FAIL]:: Could not treat some stuff of the for 10-4")},
// Many strings
{s4, emptySliceInt, s4, errors.New("[FAIL]:: Cannot process many targets")},
// One string
{s5, emptySliceInt, s5, errors.New("[FAIL]:: Cannot process a single target")},
// Many ints
{s6, []int{0, 2, 5}, emptySliceStr, errors.New("[FAIL]:: Failed to process a bunch of ints")},
{s7, []int{1}, emptySliceStr, errors.New("[FAIL]:: Could not process just one fcking int ffs")},
// Mixed
{s8, []int{0, 2, 3, 4, 5, 7}, s5, errors.New("[FAIL]:: Failed to process many mixed args (1)")},
{s9, []int{7, 2, 3, 4, 5}, s5, errors.New("[FAIL]:: Failed to process many mixed args (2)")},
{s10, []int{2, 3, 4, 5, 7}, s5, errors.New("[FAIL]:: Failed to process many mixed args (3)")},
{s11, []int{1, 2, 3, 4, 5, 6}, s4, errors.New("[FAIL]:: Failed to process many mixed args (4)")},
}

// running tests
for _, tst := range tests {
gotSlInt, gotSlStr := processArgsRm(tst.param)
if SameSlicesInt(gotSlInt, tst.wantSlInt) || SameSlices(gotSlStr, tst.wantSlStr) {
if !SameSlicesInt(gotSlInt, tst.wantSlInt) || !SameSlices(gotSlStr, tst.wantSlStr) {
fmt.Println("[GOT] -->\t", gotSlInt, gotSlStr)
fmt.Println("[WANT]-->\t", tst.wantSlInt, tst.wantSlStr)
fmt.Printf(":GOT:\t\t %T %T\n", gotSlInt, gotSlStr)
fmt.Printf(":WANT:\t\t %T %T\n", tst.wantSlInt, tst.wantSlStr)
fmt.Println("-------------------------------------------------")
t.Log(tst.err.Error())
t.Fail()
}
Expand Down

0 comments on commit fb436d1

Please sign in to comment.