1. Kink Programming Language
  2. Untitled project
  3. kink

Source

kink / src / test / kink / enhancertest / Path_test.kn

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#!/usr/bin/env kink
# vim: et sw=4 sts=4

# Copyright (c) 2013 Miyakawa Taku
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

use('TEST.test')
use('CHECK.check_thrown')
use('USING.using')
use('JAVA.byte')
use('GUARD.guarding')
use('java.nio.file.Path')
use('java.nio.file.Paths')
use('java.nio.file.Files')
use('java.nio.file.FileSystems')
use('java.nio.file.LinkOption')
use('java.io.File')
use('java.lang.Object')
use('java.lang.System')
use('java.util.HashSet')

# For conditional tests
:Unix? = ! System.getProperty('os.name').any?('Windows')
:Not_root? = System.getProperty('user.name') != 'root'

# Function to use a work directory
:using_workdir = { (:block)
    guarding { (:Sg)
        :Work_dir = Paths.get(System.getProperty('org.kink_lang.kink.tempdir')).make_temp_dir('testdir')
        Sg.on_exit { Work_dir.delete_recursively }
        block(Work_dir)
    }
}

# Get the parent path via parent_path
test {
    :Path = Paths.get('/usr/local/bin')
    Path.parent_path.check(Paths.get('/usr/local'))
}

# Get a child path via op_add with a string
test {
    :Parent = Paths.get('/usr/local')
    :Child = Parent + 'bin'
    Child.check(Paths.get('/usr/local/bin'))
}

# Get a child path via op_add with a path
test {
    :Parent = Paths.get('/usr/local')
    :Child = Parent + Paths.get('bin')
    Child.check(Paths.get('/usr/local/bin'))
}

# Get a sibling via sibling with a string
test {
    :Bin = Paths.get('/usr/local/bin')
    :Lib = Bin.sibling('lib')
    Lib.check(Paths.get('/usr/local/lib'))
}

# Get a sibling via sibling with a path
test {
    :Bin = Paths.get('/usr/local/bin')
    :Lib = Bin.sibling(Paths.get('lib'))
    Lib.check(Paths.get('/usr/local/lib'))
}

# Get a resolve via sibling with a path
test {
    :Bin = Paths.get('/usr/local/bin')
    :Lib = Bin.sibling(Paths.get('lib'))
    Lib.check(Paths.get('/usr/local/lib'))
}

# Get a relative path via op_sub with a string
test {
    :Usr_local_bin = Paths.get('/usr/local/bin')
    :Local_bin = Usr_local_bin - '/usr'
    Local_bin.check(Paths.get('local/bin'))
}

# Get a relative path via op_sub with a path
test {
    :Usr_local_bin = Paths.get('/usr/local/bin')
    :Usr = Paths.get('/usr')
    :Local_bin = Usr_local_bin - Usr
    Local_bin.check(Paths.get('local/bin'))
}

# Get the leaf from a path via leaf
test {
    :Usr_local_bin = Paths.get('/usr/local/bin')
    Usr_local_bin.leaf.check(Paths.get('bin'))
}

# Get the root of a path
test {
    Paths.get('/usr/local/bin').has_root?.check(true)
    Paths.get('/usr/local/bin').root.check(Paths.get('/'))
    Paths.get('bin/kink').has_root?.check(false)
}

# Normalize the path
test {
    Paths.get('/usr/local/bin/../lib').normalize.check(Paths.get('/usr/local/lib'))
}

# Determine if the path is absolute or not
test {
    Paths.get(System.getProperty('user.dir')).absolute?.check(true)
    Paths.get('bin').absolute?.check(false)
}

# Get the absolute path
test {
    :Relative = Paths.get('bin')
    Relative.absolute.check(Paths.get(System.getProperty('user.dir') 'bin'))
}

# Get the real path
test {
    :Relative = Paths.get('.')
    Relative.real.check(Paths.get(System.getProperty('user.dir')))
}

#
# Make files and directories
#

# Create a temporary directory
test {
    # with a prefix
    using_workdir { (:Work_dir)
        :Temp = Work_dir.make_temp_dir('tempdir')
        Temp.dir?.check(true)
        Temp.leaf.name.starts_with?('tempdir').check(true)
    }
}

test {
    # without a prefix
    using_workdir { (:Work_dir)
        :Temp = Work_dir.make_temp_dir
        Temp.dir?.check(true)
    }
}

# Create a temporary file
test {
    # with a prefix and a suffix
    using_workdir { (:Work_dir)
        :Temp = Work_dir.make_temp_file('tempfile' '.log')
        Temp.file?.check(true)
        Temp.leaf.name.starts_with?('tempfile').check(true)
        Temp.leaf.name.ends_with?('.log').check(true)
    }
}

test {
    # with a prefix
    using_workdir { (:Work_dir)
        :Temp = Work_dir.make_temp_file('tempfile')
        Temp.file?.check(true)
        Temp.leaf.name.starts_with?('tempfile').check(true)
        Temp.leaf.name.ends_with?('.tmp').check(true)
    }
}

test {
    # without a prefix nor a suffix
    using_workdir { (:Work_dir)
        :Temp = Work_dir.make_temp_file
        Temp.file?.check(true)
        Temp.leaf.name.ends_with?('.tmp').check(true)
    }
}

# Create a directory
test {
    using_workdir { (:Work_dir)
        :Dir = Work_dir + 'dir_by_make_dir'
        Dir.make_dir
        Dir.dir?.check(true)
        # Cannot create an existing directory
        check_thrown { Dir.make_dir }
    }
}

# Create directories
test {
    using_workdir { (:Work_dir)
        :Dir = Work_dir + 'parentdir/dir_by_make_dir_recursively'
        Dir.make_dir_recursively
        Dir.dir?.check(true)
        # Do nothing for an existing directory
        Dir.make_dir_recursively
    }
}

# Create a file
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_by_make_file'
        File.file?.check(false)
        File.make_file
        File.file?.check(true)
        File.bytes.check(byte.array())
    }
}

# Create a hard link
test {
    Unix?.then {
        :Bytes = byte.array(0 1 2 3)
        using_workdir { (:Work_dir)
            :File = Work_dir + 'target_file'
            :Link = Work_dir + 'link'
            File.write_bytes(Bytes)
            Link.make_link(File)
            Link.bytes.check(Bytes)
            File.delete
            Link.bytes.check(Bytes)
        }
    }
}

# Create a symbolic link
test {
    Unix?.then {
        :Bytes = byte.array(0 1 2 3)
        using_workdir { (:Work_dir)
            :File = Work_dir + 'target_file'
            :Symlink = Work_dir + 'symlink'
            File.write_bytes(Bytes)
            Symlink.make_symlink(Paths.get('target_file'))
            Symlink.symlink?.check(true)
            Symlink.symlink_target.check(Paths.get('target_file'))
            Symlink.bytes.check(Bytes)
            File.delete
            check_thrown { Symlink.bytes }
        }
    }
}

# Delete a file
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'removed_by_rm'
        File.make_file
        File.delete
        File.file?.check(false)
    }
}

# Delete a directory
test {
    using_workdir { (:Work_dir)
        :Dir = Work_dir + 'removed_by_delete'
        Dir.make_dir
        Dir.delete
        Dir.dir?.check(false)
    }
}

# Delete a directory tree
test {
    using_workdir { (:Work_dir)
        :Dir = Work_dir + 'directory'
        Dir.make_dir
        :Child_file = Dir + 'childfile'
        Child_file.make_file
        :Child_dir = Dir + 'childdir'
        Child_dir.make_dir
        Dir.delete_recursively
        Dir.dir?.check(false)
    }
}

# Delete a directory tree, but does not follow links
test {
    Unix?.then {
        using_workdir { (:Work_dir)
            :Target = Work_dir + 'target'
            Target.make_file
            :Dir = Work_dir + 'dir'
            Dir.make_dir
            :Symlink = Work_dir + 'symlink'
            Symlink.make_symlink(Paths.get('../target'))
            Dir.delete_recursively
            Dir.dir?.check(false)
            Target.file?.check(true)
        }
    }
}

#
# Exists/NotExists
#

# Test for a file
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file'
        File.exists?.check(false)
        File.not_exists?.check(true)
        File.make_file
        File.exists?.check(true)
        File.not_exists?.check(false)
    }
}

# Test for a symlink for a file
test {
    Unix?.then {
        using_workdir { (:Work_dir)
            :File = Work_dir + 'file'
            :Symlink = Work_dir + 'symlink'

            # -file -symlink
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(false)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(true)

            # -file +symlink
            Symlink.make_symlink(Paths.get('file'))
            Symlink.exists?.check(false)
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(true)
            Symlink.not_exists?.check(true)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(false)

            # +file +symlink
            File.make_file
            Symlink.exists?.check(true)
            Symlink.not_exists?.check(false)
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(true)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(false)
        }
    }
}

# Test for a directory
test {
    using_workdir { (:Work_dir)
        :Dir = Work_dir + 'directory'
        Dir.exists?.check(false)
        Dir.not_exists?.check(true)
        Dir.make_dir
        Dir.exists?.check(true)
        Dir.not_exists?.check(false)
    }
}

# Test for a symlink for a directory
test {
    Unix?.then {
        using_workdir { (:Work_dir)
            :Dir = Work_dir + 'directory'
            :Symlink = Work_dir + 'symlink'

            # -directory -symlink
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(false)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(true)

            # -directory +symlink
            Symlink.make_symlink(Paths.get('directory'))
            Symlink.exists?.check(false)
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(true)
            Symlink.not_exists?.check(true)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(false)

            # +directory +symlink
            Dir.make_dir
            Symlink.exists?.check(true)
            Symlink.not_exists?.check(false)
            Symlink.exists?(LinkOption.NOFOLLOW_LINKS).check(true)
            Symlink.not_exists?(LinkOption.NOFOLLOW_LINKS).check(false)
        }
    }
}

#
# Readable/Writable/Executable
#

# Tests if the file is readable or not.
test {
    (Unix? && Not_root?).then {
        :Bytes = byte.array(0 1 2 3 4)
        using_workdir { (:Work_dir)
            :File = Work_dir + 'file'
            File.write_bytes(Bytes)
            File.toFile.setReadable(true)
            File.readable?.check(true)
            File.bytes.check(Bytes)
            File.toFile.setReadable(false)
            File.readable?.check(false)
            check_thrown { File.bytes }
        }
    }
}

# Tests if the file is writable or not.
test {
    (! Unix? || Not_root?).then {
        :Bytes = byte.array(0 1 2 3 4)
        using_workdir { (:Work_dir)
            :File = Work_dir + 'file'
            File.make_file
            File.toFile.setWritable(true)
            File.writable?.check(true)
            File.write_bytes_appending(Bytes)
            File.bytes.check(Bytes)
            File.toFile.setWritable(false)
            check_thrown { File.write_bytes_appending(Bytes) }
            File.toFile.setWritable(true)
        }
    }
}

# Tests if the file is executable or not.
test {
    Unix?.then {
        using_workdir { (:Work_dir)
            :File = Work_dir + 'some.bat'
            File.make_file
            File.toFile.setExecutable(true)
            File.executable?.check(true)
            File.toFile.setExecutable(false)
            File.executable?.check(false)
        }
    }
}

#
# Same file or not
#
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file'
        File.make_file
        :Alias = Work_dir + '.' + 'file'
        :Other = Work_dir + 'otherfile'
        Other.make_file
        File.same_path_as?(Alias).check(true)
        File.same_path_as?(Other).check(false)
    }
}

#
# Hidden or not
#
test {
    Unix?.then {
        using_workdir { (:Work_dir)
            :Hidden = Work_dir + '.hidden'
            :Not_hidden = Work_dir + 'not_hidden'
            Hidden.make_file
            Not_hidden.make_file
            Hidden.hidden?.check(true)
            Not_hidden.hidden?.check(false)
        }
    }
}

#
# Streams
#

# Output stream
test {
    :First = byte.array(1 2 3 4 5)
    :Second = byte.array(6 7 8 9 10)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_for_bytes'
        using(File.output_stream) { (:Out)
            Out.write(First)
        }
        File.bytes.check(First)
        using(File.output_stream) { (:Out)
            Out.write(Second)
        }
        File.bytes.check(Second)
    }
}

# Output stream for append
test {
    :First = byte.array(1 2 3 4 5)
    :Second = byte.array(6 7 8 9 10)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_for_bytes'
        using(File.output_stream_appending) { (:Out)
            Out.write(First)
        }
        File.bytes.check(First)
        using(File.output_stream_appending) { (:Out)
            Out.write(Second)
        }
        File.bytes.check(byte.array(*** First.list *** Second.list))
    }
}

# Input stream
test {
    :Bytes = byte.array(0 1 2 3 4)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file'
        File.write_bytes(Bytes)
        using(File.input_stream) { (:In)
            In.bytes.check(Bytes)
        }
    }
}

#
# Write and read contents of a file
#

# Create a new file containing bytes via 'write_bytes' and read bytes from the file
test {
    :First = byte.array(1 2 3 4 5)
    :Second = byte.array(6 7 8 9 10)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_of_bytes'
        # Create a new file
        :Result = File.write_bytes(First)
        Result.check(File)
        File.bytes.check(First)
        # The content is truncated
        :Result = File.write_bytes(Second)
        Result.check(File)
        File.bytes.check(Second)
    }
}

# Create a new file containing bytes via 'write_bytes_appending' and read bytes from the file
test {
    :First = byte.array(1 2 3 4 5)
    :Second = byte.array(6 7 8 9 10)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_of_bytes'
        # Create a new file
        :Result = File.write_bytes_appending(First)
        Result.check(File)
        File.bytes.check(First)
        # The content is truncated
        :Result = File.write_bytes_appending(Second)
        Result.check(File)
        File.bytes.check(byte.array(*** First.list *** Second.list))
    }
}

# Create a new file containing bytes via 'write_bytes' and read partial bytes from the file
test {
    :Bytes = byte.array(1 2 3 4 5 6 7 8 9 10)
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file_of_bytes'
        # Create a new file
        :Result = File.write_bytes(Bytes)
        Result.check(File)
        File.bytes(5).check(Bytes.slice(0 5))
        File.bytes(20).check(Bytes)
    }
}

# print, print_appending and text
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file'
        File.print('twift')
        File.print_appending('voxxx')
        File.text.check('twift' + 'voxxx')
        File.print('bazbaz' 'UTF-8')
        File.print_appending('zigzig' 'UTF-8')
        File.text('UTF-8').check('bazbaz' + 'zigzig')
    }
}

# print_line, print_line_appending and lines
test {
    using_workdir { (:Work_dir)
        :File = Work_dir + 'file'
        File.print_line('twift')
        File.print_line_appending('voxxx')
        File.print_line_appending('peng')
        File.lines.map { \0.chomp } .check(['twift' 'voxxx' 'peng'])
        File.print_line('bazbaz' 'UTF-8')
        File.print_line_appending('zigzig' 'UTF-8')
        File.lines('UTF-8').map { \0.chomp } .check(['bazbaz' 'zigzig'])
    }
}

#
# Read a file as a program
#

# Read a file as a program, without a charset (default to UTF-8)
test {
    using_workdir { (:Work_dir)
        :Program_kn = Work_dir + 'program.kn'
        :Text = 'print_line("Hello!")'
        Program_kn.print(Text 'UTF-8')
        :Pg = Program_kn.program
        Pg.check(Text.program(Program_kn.name))
    }
}

#
# Find descendants
#

# Get child paths
test {
    using_workdir { (:Work_dir)
        :A = Work_dir + 'a'
        :B = Work_dir + 'b'
        :C = Work_dir + 'c'
        :D = Work_dir + 'd'
        A.make_file
        B.make_file
        C.make_file
        D.make_file
        HashSet.new(Work_dir.child_paths).check(HashSet.new([A B C D]))
    }
}

# Get a tree of a directory
test {
    using_workdir { (:Work_dir)
        :Src = Work_dir + 'src'
        Src.make_dir
        :Main_c = Src + 'main.c'
        Main_c.make_file
        :Util_c = Src + 'util.c'
        Util_c.make_file
        :Test = Work_dir + 'test'
        Test.make_dir
        :Ut_c = Test + 'ut.c'
        Ut_c.make_file
        HashSet.new(Work_dir.tree).check(HashSet.new([Work_dir Src Main_c Util_c Test Ut_c]))
    }
}

# Convert a path to a path
test {
    :Path_value = Paths.get('/usr/local/bin')
    Path_value.can_into?(Path).check(true)
    Path_value.into(Path).check(Path_value)
}

# Convert a path to its concrete class
test {
    :Path = Paths.get('/usr/local/bin')
    Path.can_into?(Path.getClass).check(true)
    Path.into(Path.getClass).check(Path)
}

# Convert a path to an object
test {
    :Path = Paths.get('/usr/local/bin')
    Path.can_into?(Object).check(true)
    Path.into(Object).check(Path)
}

# Convert a path to a file
test {
    :Path = Paths.get('/usr/local/bin')
    Path.can_into?(File).check(true)
    Path.into(File).check(File.new('/usr/local/bin'))
}

# Cannot convert the prototype to a path
test {
    Path.prototype.can_into?(Path).check(false)
}

# Get a string representation of a path
test {
    :Sep = FileSystems.getDefault.getSeparator
    Paths.get('/usr/local/bin').name.check('/usr/local/bin'.gsub('/' Sep))
}

# Get a evaluatable string representation of a path
test {
    :Sep = FileSystems.getDefault.getSeparator
    Paths.get('/usr/local/bin').show.check('path(' + '/usr/local/bin'.gsub('/' Sep).show + ')')
}

# PROTOTYPE.show looks different
test {
    :Sep = FileSystems.getDefault.getSeparator
    Path.prototype.show.check('null-path')
}

# Test PATH.path??
test {
    path('.').path??.check(true)
}

test {
    Path.prototype.path??.check(false)
}