@@ -109,3 +109,43 @@ Deno.test("HashMap.from() builds map from pairs", () => {
109109 expect ( map . size ( ) ) . toBe ( 3 ) ;
110110 expect ( map . get ( "b" ) ) . toEqual ( some ( 2 ) ) ;
111111} ) ;
112+
113+ Deno . test ( "HashMap: entry().orInsert inserts if missing" , ( ) => {
114+ const map = new HashMap < string , number > ( ) ;
115+ const entry = map . entry ( "x" ) ;
116+ entry . orInsert ( 42 ) ;
117+
118+ expect ( map . get ( "x" ) ) . toEqual ( some ( 42 ) ) ;
119+ } ) ;
120+
121+ Deno . test ( "HashMap: entry().orInsertWith inserts lazily" , ( ) => {
122+ const map = new HashMap < string , number > ( ) ;
123+ let called = false ;
124+ const entry = map . entry ( "x" ) ;
125+ entry . orInsertWith ( ( ) => {
126+ called = true ;
127+ return 99 ;
128+ } ) ;
129+
130+ expect ( called ) . toBe ( true ) ;
131+ expect ( map . get ( "x" ) ) . toEqual ( some ( 99 ) ) ;
132+ } ) ;
133+
134+ Deno . test ( "HashMap: entry().andModify modifies if present" , ( ) => {
135+ const map = new HashMap < string , number > ( ) ;
136+ map . set ( "x" , 10 ) ;
137+ map . entry ( "x" ) . andModify ( ( v ) => v += 5 ) ;
138+ map . entry ( "y" ) . andModify ( ( v ) => v += 5 ) ;
139+
140+ expect ( map . get ( "x" ) ) . toEqual ( some ( 15 ) ) ;
141+ expect ( map . get ( "y" ) ) . toEqual ( none ( ) ) ;
142+ } ) ;
143+
144+ Deno . test ( "HashMap: Entry.remove() removes entry" , ( ) => {
145+ const map = new HashMap < string , string > ( ) ;
146+ map . set ( "hello" , "world" ) ;
147+ const entry = map . entry ( "hello" ) ;
148+ expect ( entry . remove ( ) ) . toEqual ( some ( "world" ) ) ;
149+ expect ( map . has ( "hello" ) ) . toBe ( false ) ;
150+ expect ( map . entry ( "bonjour" ) . remove ( ) ) . toEqual ( none ( ) ) ;
151+ } ) ;
0 commit comments