Skip to content
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

Add relationships for python poetry packages #2906

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package internal
const (
// JSONSchemaVersion is the current schema version output by the JSON encoder
// This is roughly following the "SchemaVer" guidelines for versioning the JSON schema. Please see schema/json/README.md for details on how to increment.
JSONSchemaVersion = "16.0.12"
JSONSchemaVersion = "16.0.13"
)
68 changes: 68 additions & 0 deletions internal/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package internal

import (
"fmt"
"sort"
)

// Set represents a generic set type.
type Set[T comparable] map[T]struct{}

// NewSet creates a new empty Set.
func NewSet[T comparable](start ...T) Set[T] {
ret := make(Set[T])
for _, v := range start {
ret.Add(v)
}
return ret
}

// Add adds elements to the set.
func (s Set[T]) Add(elements ...T) {
for _, e := range elements {
s[e] = struct{}{}
}
}

// Remove removes an element from the set.
func (s Set[T]) Remove(element T) {
delete(s, element)
}

// Contains checks if an element is in the set.
func (s Set[T]) Contains(element T) bool {
_, ok := s[element]
return ok
}

// ToSlice returns a sorted slice of elements in the set.
func (s Set[T]) ToSlice() []T {
ret := make([]T, len(s))
idx := 0
for v := range s {
ret[idx] = v
idx++
}
sort.Slice(ret, func(i, j int) bool {
return fmt.Sprintf("%v", ret[i]) < fmt.Sprintf("%v", ret[j])
})
return ret
}

// Equals checks if two sets are equal.
func (s Set[T]) Equals(o Set[T]) bool {
if len(s) != len(o) {
return false
}
for k := range s {
if !o.Contains(k) {
return false
}
}
return true
}

// Empty checks if the set is empty.
func (s Set[T]) Empty() bool {
return len(s) == 0
}
146 changes: 146 additions & 0 deletions internal/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package internal

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewSet(t *testing.T) {
tests := []struct {
name string
start []int
result Set[int]
}{
{"empty set", []int{}, NewSet[int]()},
{"non-empty set", []int{1, 2, 3}, NewSet(1, 2, 3)},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.start...)
require.Equal(t, tt.result, s)
})
}
}

func TestAdd(t *testing.T) {
tests := []struct {
name string
input []int
add []int
result Set[int]
}{
{"add to empty set", []int{}, []int{1, 2, 3}, NewSet(1, 2, 3)},
{"add to non-empty set", []int{1}, []int{2, 3}, NewSet(1, 2, 3)},
{"add existing elements", []int{1, 2}, []int{2, 3}, NewSet(1, 2, 3)},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.input...)
s.Add(tt.add...)
assert.Equal(t, tt.result, s)
})
}
}

func TestRemove(t *testing.T) {
tests := []struct {
name string
input []int
remove int
result Set[int]
}{
{"remove from non-empty set", []int{1, 2, 3}, 2, NewSet(1, 3)},
{"remove non-existent element", []int{1, 2}, 3, NewSet(1, 2)},
{"remove from single-element set", []int{1}, 1, NewSet[int]()},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.input...)
s.Remove(tt.remove)
assert.Equal(t, tt.result, s)
})
}
}

func TestContains(t *testing.T) {
tests := []struct {
name string
input []int
contains int
result bool
}{
{"element in set", []int{1, 2, 3}, 2, true},
{"element not in set", []int{1, 2}, 3, false},
{"empty set", []int{}, 1, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.input...)
assert.Equal(t, tt.result, s.Contains(tt.contains))
})
}
}

func TestToSlice(t *testing.T) {
tests := []struct {
name string
input []int
result []int
}{
{"non-empty set", []int{3, 1, 2}, []int{1, 2, 3}},
{"empty set", []int{}, []int{}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.input...)
assert.Equal(t, tt.result, s.ToSlice())
})
}
}

func TestEquals(t *testing.T) {
tests := []struct {
name string
set1 []int
set2 []int
result bool
}{
{"equal sets", []int{1, 2, 3}, []int{3, 2, 1}, true},
{"different sets", []int{1, 2}, []int{2, 3}, false},
{"empty sets", []int{}, []int{}, true},
{"one empty set", []int{1, 2}, []int{}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s1 := NewSet(tt.set1...)
s2 := NewSet(tt.set2...)
assert.Equal(t, tt.result, s1.Equals(s2))
})
}
}

func TestEmpty(t *testing.T) {
tests := []struct {
name string
input []int
result bool
}{
{"non-empty set", []int{1}, false},
{"empty set", []int{}, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet(tt.input...)
assert.Equal(t, tt.result, s.Empty())
})
}
}
61 changes: 0 additions & 61 deletions internal/stringset.go

This file was deleted.

Loading
Loading