|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { DataFrame } from '../../../src/core/DataFrame.js'; |
| 3 | +import { print } from '../../../src/methods/display/print.js'; |
| 4 | + |
| 5 | +describe('DataFrame print method', () => { |
| 6 | + // Create test data frame |
| 7 | + const testData = [ |
| 8 | + { name: 'Alice', age: 25, city: 'New York' }, |
| 9 | + { name: 'Bob', age: 30, city: 'Boston' }, |
| 10 | + { name: 'Charlie', age: 35, city: 'Chicago' }, |
| 11 | + { name: 'David', age: 40, city: 'Denver' }, |
| 12 | + { name: 'Eve', age: 45, city: 'El Paso' }, |
| 13 | + ]; |
| 14 | + |
| 15 | + const df = DataFrame.create(testData); |
| 16 | + |
| 17 | + it('should format data as a table string', () => { |
| 18 | + // Mock console.log to check output |
| 19 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 20 | + |
| 21 | + // Call print function directly |
| 22 | + const printFn = print(); |
| 23 | + printFn(df._frame); |
| 24 | + |
| 25 | + // Check that console.log was called |
| 26 | + expect(consoleSpy).toHaveBeenCalled(); |
| 27 | + |
| 28 | + // Get the argument passed to console.log |
| 29 | + const output = consoleSpy.mock.calls[0][0]; |
| 30 | + |
| 31 | + // Check that the output contains column headers |
| 32 | + expect(output).toContain('name'); |
| 33 | + expect(output).toContain('age'); |
| 34 | + expect(output).toContain('city'); |
| 35 | + |
| 36 | + // Check that the output contains data |
| 37 | + expect(output).toContain('Alice'); |
| 38 | + expect(output).toContain('25'); |
| 39 | + expect(output).toContain('New York'); |
| 40 | + |
| 41 | + // Restore console.log |
| 42 | + consoleSpy.mockRestore(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return the frame for method chaining', () => { |
| 46 | + // Mock console.log |
| 47 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 48 | + |
| 49 | + // Call print function directly |
| 50 | + const printFn = print(); |
| 51 | + const result = printFn(df._frame); |
| 52 | + |
| 53 | + // Check that the function returns the frame |
| 54 | + expect(result).toBe(df._frame); |
| 55 | + |
| 56 | + // Restore console.log |
| 57 | + consoleSpy.mockRestore(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should respect maxRows option', () => { |
| 61 | + // Create a frame with many rows |
| 62 | + const largeData = Array.from({ length: 20 }, (_, i) => ({ |
| 63 | + id: i, |
| 64 | + value: i * 10, |
| 65 | + })); |
| 66 | + |
| 67 | + const largeDf = DataFrame.create(largeData); |
| 68 | + |
| 69 | + // Mock console.log |
| 70 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 71 | + |
| 72 | + // Call print function with row limit |
| 73 | + const printFn = print(); |
| 74 | + printFn(largeDf._frame, { maxRows: 5 }); |
| 75 | + |
| 76 | + // Get the output |
| 77 | + const output = consoleSpy.mock.calls[0][0]; |
| 78 | + |
| 79 | + // Check that the output contains message about additional rows |
| 80 | + expect(output).toContain('more rows'); |
| 81 | + |
| 82 | + // Restore console.log |
| 83 | + consoleSpy.mockRestore(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should respect maxCols option', () => { |
| 87 | + // Create a frame with many columns |
| 88 | + const wideData = [{ col1: 1, col2: 2, col3: 3, col4: 4, col5: 5, col6: 6 }]; |
| 89 | + |
| 90 | + const wideDf = DataFrame.create(wideData); |
| 91 | + |
| 92 | + // Mock console.log |
| 93 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 94 | + |
| 95 | + // Call print function with column limit |
| 96 | + const printFn = print(); |
| 97 | + printFn(wideDf._frame, { maxCols: 3 }); |
| 98 | + |
| 99 | + // Get the output |
| 100 | + const output = consoleSpy.mock.calls[0][0]; |
| 101 | + |
| 102 | + // Check that the output contains message about additional columns |
| 103 | + expect(output).toContain('more columns'); |
| 104 | + |
| 105 | + // Restore console.log |
| 106 | + consoleSpy.mockRestore(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments