Giter Club home page Giter Club logo

Comments (4)

patrick-hermann-sva avatar patrick-hermann-sva commented on August 16, 2024

another example!

mkdir ankit3 && cd ankit3
cat <<EOF > ankit3.go
package main

import "fmt"

func removeDuplicateStr(results []string) []string {
	allKeys := make(map[string]bool)
	list := []string{}
	for _, item := range results {
		if _, value := allKeys[item]; !value {
			allKeys[item] = true
			list = append(list, item)
		}
	}
	return list
}

func main() {

	testData := []string{"Kind", "Name", "Name"}
	testOutput := removeDuplicateStr(testData)
	fmt.Println(testOutput)

}
EOF

cat <<EOF > ankit3_test.go
package main

import (
	"fmt"
	"reflect"
	"testing"
)

func TestRemoveDuplicateStr(t *testing.T) {

	type test struct {
		testInput []string
		want      []string
	}

	tests := []test{
		{testInput: []string{"Kind", "Name", "Name"}, want: []string{"Kind", "Name"}},
	}

	for _, tc := range tests {

		removed := removeDuplicateStr(tc.testInput)

		fmt.Println(removed)
		fmt.Println(tc.want)

		if !reflect.DeepEqual(removed, tc.want) {
			t.Errorf("Slices are not equal, BUT we expected them to be...")
		}

	}

}

EOF

go mod init ankit
go mod tidy

go run ankit.go
go test -v

from stagetime-creator.

ankitsharma12-ops avatar ankitsharma12-ops commented on August 16, 2024

second tests case

mkdir ankit4
cat << EOF > ankit4.go
package main

import (
	"fmt"
	"regexp"
)

func main() {
	//
		scanText := "{{ .Kind }}"
		testPattern := `\{\{(.*?)\}\}`
		testOutput := GetAllRegexMatches(scanText, testPattern)
		fmt.Println(testOutput)
		
	//
}
func GetAllRegexMatches(scanText, regexPattern string) []string {

	re := regexp.MustCompile(regexPattern)
	fmt.Println(re)
	return re.FindAllString(scanText, -1)
	
}
EOF

cat <<EOF > ankit3_test.go
package main

import (
	"testing"
	"reflect"
	"fmt"
)

func TestGetRegexMatch(t *testing.T) {
	type test struct {
		scanText string
		testPattern string
		want      []string
	}

	tests := []test{
		{scanText: "whatever {{ .Kind1 }}", testPattern: `\{\{(.*?)\}\}`, want: []string{"{{ .Kind1 }}"}},
		{scanText: "{{ .Name }}", testPattern: `\{\{(.*?)\}\}`, want: []string{"{{ .Name }}"}},
	}
	for _, tc := range tests {

		scanresult := GetAllRegexMatches(tc.scanText, tc.testPattern)
		fmt.Println(scanresult)
		fmt.Println(reflect.DeepEqual(scanresult, tc.want))
		if !reflect.DeepEqual(scanresult, tc.want) {
			t.Errorf("error")
	
		}	
	}
}
EOF

and test it, it works like this:

shq1kor@BMH2-C-00134:~/development/projects/go/src/ankit1$ go test -v
=== RUN   TestGetRegexMatch
\{\{(.*?)\}\}
[{{ .Kind1 }}]
true
\{\{(.*?)\}\}
[{{ .Name }}]
true
--- PASS: TestGetRegexMatch (0.00s)
PASS
ok      ankit   0.002s

from stagetime-creator.

patrick-hermann-sva avatar patrick-hermann-sva commented on August 16, 2024

@ankitsharma12-ops TASK1

Update the following code that we have at the end trimedResults w/ Name and Kind1 (WITHOUT . + WITHOUT {{ and WITHOUT }})

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"reflect"
	"regexp"
	"strings"
	"testing"
)

func TestGetRegexMatch(t *testing.T) {
	type test struct {
		scanText    string
		testPattern string
		want        []string
	}

	tests := []test{
		{scanText: "whatever {{ .Kind1 }}", testPattern: `\{\{(.*?)\}\}`, want: []string{"{{ .Kind1 }}"}},
		{scanText: "{{ .Name }}", testPattern: `\{\{(.*?)\}\}`, want: []string{"{{ .Name }}"}},
	}

	for _, tc := range tests {

		scanresult := GetAllRegexMatches(tc.scanText, tc.testPattern)
		fmt.Println(scanresult)

		// RESULT HERE IS {{ .Kind1 }} BUUUUT WE NEED JUST Kind1 (WITHOUT . + WITHOUT {{ and WITHOUT }})

		var trimedResults []string

		for _, result := range scanresult {
			fmt.Println("THIS IS INSIDE THE REULTS")
			fmt.Println(result)

			fmt.Println("THIS IS WITHOUT THE DOT")
			woDot := strings.Replace(result, ".", "", -1)
			fmt.Println(woDot)

			// REPLACE {{
			// REPLACE }}
			// TRIM SPACE

			trimedResults = append(trimedResults, woDot)
		}

		fmt.Println("THIS IS ARE THE RESULTS WE CARE FOR..")
		fmt.Println(trimedResults)

		fmt.Println(reflect.DeepEqual(scanresult, tc.want))
		if !reflect.DeepEqual(scanresult, tc.want) {
			t.Errorf("error")

		}
	}

}

func GetAllRegexMatches(scanText, regexPattern string) []string {

	re := regexp.MustCompile(regexPattern)
	fmt.Println(re)
	return re.FindAllString(scanText, -1)

}


from stagetime-creator.

patrick-hermann-sva avatar patrick-hermann-sva commented on August 16, 2024

@ankitsharma12-ops

package main

import "fmt"

func main() {

	// new maps
	ankit := make(map[string]string)
	boss := make(map[string]string)

	ankit["food"] = "lamb"
	ankit["team"] = "manu"
	ankit["tool"] = "k9s"

	boss["food"] = "chicken"
	boss["team"] = "stuttgart"

	// FIND A WAY TO COMPARE BOTH MAPS -> BOTH OF THEM SHOULD CONTAIN THE SAME KEYS ( WE DO NOT CARE ABOUT THE VALUES - JUST THE KEYS)

	// OR YOU COMARE ONE MAP INSIDE A LOOP WITH A STRING SLICE - ALSO OK!
	keys := []string{"food", "team", "tool"}
	for _, key := range keys {
		fmt.Println(key)
		fmt.Println(boss[key])
		if boss[key] == "" {
			fmt.Println("Looks bad - no key for boss")
		}
	}

}

from stagetime-creator.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.